Skip to content

Instantly share code, notes, and snippets.

@KangHidro
Created February 9, 2026 03:42
Show Gist options
  • Select an option

  • Save KangHidro/ce6f41787449259948ebafdaddda83f4 to your computer and use it in GitHub Desktop.

Select an option

Save KangHidro/ce6f41787449259948ebafdaddda83f4 to your computer and use it in GitHub Desktop.
Angular 12- Google Login
...
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);
}
...
...
<head>
...
<script src="https://accounts.google.com/gsi/client" defer></script>
...
</head>
...
...
<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>
...
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
...
}
);
}
}
);
}
...
}
...
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