Created
February 9, 2026 03:42
-
-
Save KangHidro/ce6f41787449259948ebafdaddda83f4 to your computer and use it in GitHub Desktop.
Angular 12- Google Login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| getGoogleUser(accessToken: string): Observable<PeopleGoogle> { | |
| const httpOptions = { | |
| headers: new HttpHeaders({ | |
| 'Content-Type': 'application/json', | |
| Authorization: 'Bearer ' + accessToken | |
| }) | |
| }; | |
| return this._http.get<PeopleGoogle> | |
| ('https://people.googleapis.com/v1/people/me?personFields=names,photos,emailAddresses', httpOptions); | |
| } | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| <head> | |
| ... | |
| <script src="https://accounts.google.com/gsi/client" defer></script> | |
| ... | |
| </head> | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| <button type="submit" class="btn btn-block btn-social btn-fill btn-google" (click)="loginGoogle()"> | |
| <span class="fa fa-google"></span> Login with Google | |
| </button> | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {... | |
| ... | |
| declare var google: any; | |
| @Component({ | |
| ... | |
| export class LoginComponent implements AfterViewInit { | |
| ggClient: any; | |
| ... | |
| constructor( | |
| private _authUserService: AuthUserService, | |
| ... | |
| ngAfterViewInit(): void { | |
| this.ggClient = google.accounts.oauth2.initTokenClient({ | |
| client_id: SystemConstant.KEY, | |
| scope: 'openid profile email', | |
| callback: (res) => { | |
| this.onLoginWithGoogle(res.access_token); | |
| } | |
| }); | |
| } | |
| loginGoogle() { | |
| this.ggClient.requestAccessToken(); | |
| } | |
| onLoginWithGoogle(accessToken: string) { | |
| this._authUserService.getGoogleUser(accessToken).subscribe( | |
| (user) => { | |
| if (user != null) { | |
| let userGoogle: UserGoogle = new UserGoogle(); | |
| userGoogle.name = user.names[0].displayName; | |
| userGoogle.photoUrl = user.photos[0].url; | |
| ... | |
| this._authUserService.doLoginWithGoogle(accessToken).subscribe( | |
| (res) => { | |
| ... | |
| // Login Success | |
| ... | |
| } | |
| ); | |
| } | |
| } | |
| ); | |
| } | |
| ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| export class PeopleGoogle { | |
| resourceName: string; | |
| names: { displayName: string; }[]; | |
| photos: { url: string; }[]; | |
| emailAddresses: { value: string; }[]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment