Browse Source

Added google single sign-on

master
Quildra 1 year ago
parent
commit
785f3626b5
  1. 2
      src/app/app.routes.ts
  2. 8
      src/app/auth.service.ts
  3. 0
      src/app/google-callback/google-callback.component.css
  4. 1
      src/app/google-callback/google-callback.component.html
  5. 23
      src/app/google-callback/google-callback.component.spec.ts
  6. 30
      src/app/google-callback/google-callback.component.ts
  7. 3
      src/app/login/login.component.html
  8. 4
      src/app/login/login.component.ts

2
src/app/app.routes.ts

@ -1,9 +1,11 @@
import { Routes } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { GoogleCallbackComponent } from './google-callback/google-callback.component';
export const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'google-callback', component: GoogleCallbackComponent },
// Add other routes here
];

8
src/app/auth.service.ts

@ -18,4 +18,12 @@ export class AuthService {
register(name: string, email: string, password: string): Observable<any> {
return this.http.post<any>(`${this.apiUrl}/register`, { name, email, password });
}
getGoogleAuthUrl(): string {
return `${this.apiUrl}/google`;
}
handleGoogleCallback(code: string): Observable<any> {
return this.http.get<any>(`${this.apiUrl}/google/callback?code=${code}`);
}
}

0
src/app/google-callback/google-callback.component.css

1
src/app/google-callback/google-callback.component.html

@ -0,0 +1 @@
<p>google-callback works!</p>

23
src/app/google-callback/google-callback.component.spec.ts

@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { GoogleCallbackComponent } from './google-callback.component';
describe('GoogleCallbackComponent', () => {
let component: GoogleCallbackComponent;
let fixture: ComponentFixture<GoogleCallbackComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [GoogleCallbackComponent]
})
.compileComponents();
fixture = TestBed.createComponent(GoogleCallbackComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

30
src/app/google-callback/google-callback.component.ts

@ -0,0 +1,30 @@
// src/app/google-callback/google-callback.component.ts
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../auth.service';
import { Router, ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-google-callback',
template: '<p>Loading...</p>',
styles: []
})
export class GoogleCallbackComponent implements OnInit {
constructor(
private authService: AuthService,
private router: Router,
private route: ActivatedRoute
) {}
ngOnInit(): void {
this.route.queryParams.subscribe(params => {
console.log("callback-hit");
const token = params['token'];
if (token) {
localStorage.setItem('token', token);
this.router.navigate(['/']);
} else {
this.router.navigate(['/login']);
}
});
}
}

3
src/app/login/login.component.html

@ -1,4 +1,3 @@
<!-- src/app/login/login.component.html -->
<h2>Login</h2>
<form (ngSubmit)="login()">
<label for="email">Email:</label>
@ -9,3 +8,5 @@
<button type="submit">Login</button>
</form>
<button (click)="googleLogin()">Login with Google</button>

4
src/app/login/login.component.ts

@ -30,4 +30,8 @@ export class LoginComponent {
err => console.error(err)
);
}
googleLogin() {
window.location.href = this.authService.getGoogleAuthUrl();
}
}

Loading…
Cancel
Save