Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit 292f0e3

Browse files
committed
Angular: 新增前端驗證Token的範例,並補上註解
1 parent 4550efa commit 292f0e3

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

Angular/.vscode/settings.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@
1717
"editor.formatOnSave": true,
1818
"editor.codeActionsOnSave": {
1919
"source.fixAll.eslint": true
20-
}
20+
},
21+
"cSpell.words": [
22+
"ASPNET",
23+
"oidc",
24+
"signin"
25+
]
2126
}

Angular/src/app/app.component.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,27 @@ export class AppComponent implements OnInit {
2222
this.route.queryParamMap.subscribe((params: ParamMap) => {
2323
this.idToken = params.get('idToken') || '';
2424
if (this.idToken) {
25-
this.authenticationService.getUser(this.idToken).subscribe((res) => {
25+
// 直接解開JWT Token
26+
this.authenticationService.extractToken(this.idToken).subscribe((res) => {
2627
this.user = res;
2728
});
29+
30+
// 如果需要在前端驗證Token,可以使用這個Google API
31+
this.authenticationService.verifyToken(this.idToken).subscribe({
32+
next: (data) => {
33+
console.log('This is verified by Google', data);
34+
},
35+
error: (error) => {
36+
console.log(error);
37+
},
38+
});
39+
40+
// TODO: 接下來可以將idToken存到localStorage,並且在每次發送API請求時,將idToken放到Authorization Header。後端只要用一樣的方式驗證idToken即可確認身份。
2841
}
2942
});
3043
}
3144

45+
// 按鈕
3246
signInWithGoogle() {
3347
this.authenticationService.oidcLogin();
3448
}

Angular/src/app/app.module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { NgModule } from '@angular/core';
22
import { BrowserModule } from '@angular/platform-browser';
3+
import { HttpClientModule } from '@angular/common/http';
34

45
import { AppRoutingModule } from './app-routing.module';
56
import { AppComponent } from './app.component';
67
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
78

89
@NgModule({
910
declarations: [AppComponent],
10-
imports: [BrowserModule, AppRoutingModule, NgbModule],
11+
imports: [BrowserModule, HttpClientModule, AppRoutingModule, NgbModule],
1112
providers: [],
1213
bootstrap: [AppComponent],
1314
})

Angular/src/app/authentication.service.ts

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { Injectable } from '@angular/core';
2-
import { Router } from '@angular/router';
32
import { environment } from 'src/environments/environment';
43

54
import jwtDecode from 'jwt-decode';
65
import { Observable, of } from 'rxjs';
6+
import { HttpClient } from '@angular/common/http';
77

88
@Injectable({
99
providedIn: 'root',
1010
})
1111
export class AuthenticationService {
12-
constructor(private router: Router) {}
12+
constructor(private http: HttpClient) {}
1313

14+
/**
15+
* 發起 Auth code flow 登入流程
16+
*
17+
* @memberof AuthenticationService
18+
*/
1419
oidcLogin(): void {
1520
const client: google.accounts.oauth2.CodeClient = google.accounts.oauth2.initCodeClient({
1621
client_id: environment.clientId,
@@ -22,7 +27,20 @@ export class AuthenticationService {
2227
client.requestCode();
2328
}
2429

25-
getUser(idToken: string): Observable<{
30+
/**
31+
* 直接解開JWT Token
32+
*
33+
* @param {string} idToken
34+
* @return {*} {Observable<{
35+
* userID: string;
36+
* userName: string;
37+
* id: string;
38+
* picture: string;
39+
* email: string;
40+
* }>}
41+
* @memberof AuthenticationService
42+
*/
43+
extractToken(idToken: string): Observable<{
2644
userID: string;
2745
userName: string;
2846
id: string;
@@ -39,4 +57,17 @@ export class AuthenticationService {
3957
email,
4058
});
4159
}
60+
61+
/**
62+
* 使用Google的API來驗證Token。參考文件: https://developers.google.com/identity/sign-in/web/backend-auth#calling-the-tokeninfo-endpoint
63+
*
64+
* @param {string} idToken
65+
* @return {*}
66+
* @memberof AuthenticationService
67+
*/
68+
verifyToken(idToken: string): Observable<unknown> {
69+
return this.http.get('https://oauth2.googleapis.com/tokeninfo', {
70+
params: { id_token: idToken },
71+
});
72+
}
4273
}

0 commit comments

Comments
 (0)