Skip to content

Commit 55e81c4

Browse files
authored
Merge pull request #47 from descope-sample-apps/chris-one-tap
Added one tap
2 parents 45b55d5 + e8b16f8 commit 55e81c4

File tree

4 files changed

+82
-15
lines changed

4 files changed

+82
-15
lines changed

package.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
},
1111
"private": true,
1212
"dependencies": {
13-
"@angular/animations": "^17.0.0",
14-
"@angular/common": "^17.0.0",
15-
"@angular/compiler": "^17.0.0",
16-
"@angular/core": "^17.0.0",
17-
"@angular/forms": "^17.0.0",
18-
"@angular/platform-browser": "^17.0.0",
19-
"@angular/platform-browser-dynamic": "^17.0.0",
20-
"@angular/router": "^17.0.0",
21-
"@descope/angular-sdk": "0.5.0-alpha.2",
13+
"@angular/animations": "^18.2.4",
14+
"@angular/common": "^18.2.4",
15+
"@angular/compiler": "^18.2.4",
16+
"@angular/core": "^18.2.4",
17+
"@angular/forms": "^18.2.4",
18+
"@angular/platform-browser": "^18.2.4",
19+
"@angular/platform-browser-dynamic": "^18.2.4",
20+
"@angular/router": "^18.2.4",
21+
"@descope/angular-sdk": "0.6.0",
2222
"angular-google-charts": "^2.2.3",
2323
"autoprefixer": "^10.4.17",
2424
"rxjs": "~7.8.0",
@@ -27,8 +27,8 @@
2727
},
2828
"devDependencies": {
2929
"@angular-builders/custom-webpack": "^18.0.0",
30-
"@angular-devkit/build-angular": "^18.0.0",
31-
"@angular/cli": "~18.1.0",
30+
"@angular-devkit/build-angular": "^18.2.4",
31+
"@angular/cli": "~18.2.4",
3232
"@angular/compiler-cli": "^18.0.0",
3333
"@types/jasmine": "~5.1.0",
3434
"jasmine-core": "~5.2.0",

src/app/app.component.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
2+
import { OneTapService } from './one-tap.service';
3+
import { Router } from '@angular/router';
24

35
@Component({
46
selector: 'app-root',
57
templateUrl: './app.component.html',
68
styleUrls: ['./app.component.css']
79
})
8-
export class AppComponent {
9-
title = 'my-angular-app';
10+
export class AppComponent implements OnInit {
11+
constructor(private oneTapService: OneTapService, private router: Router) {}
1012

13+
async ngOnInit(): Promise<void> {
14+
// Check if the user is authenticated
15+
const isAuthenticated = this.oneTapService.isAuthenticated();
16+
17+
if (!isAuthenticated) {
18+
// If the user is not authenticated and One Tap is not initialized, show One Tap
19+
if (!this.oneTapService.isOneTapInitialized()) {
20+
await this.oneTapService.displayOneTap();
21+
}
22+
} else {
23+
// If user is authenticated, only redirect to dashboard if not already there
24+
if (this.router.url !== '/dashboard') {
25+
this.router.navigate(['/dashboard']); // Use Angular router for navigation
26+
}
27+
}
28+
}
1129
}

src/app/home/home.component.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export class HomeComponent {
2020
role: descopeUser.user.roleNames || 'No Role Set',
2121
picture: descopeUser.user.picture || '',
2222
};
23-
}
23+
} else {
24+
// If the user is logged out, clear the user details
25+
this.user = null;
26+
// Optionally, trigger One Tap or another login flow here
27+
}
2428
});
2529
}
2630
}

src/app/one-tap.service.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Injectable } from '@angular/core';
2+
import Descope from '@descope/web-js-sdk';
3+
import { environment } from '../environments/environment';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class OneTapService {
9+
private sdk: any;
10+
private oneTapInitialized: boolean = false;
11+
12+
constructor() {
13+
const projectId = environment.descopeProjectId;
14+
this.sdk = Descope({ projectId: projectId, persistTokens: true, autoRefresh: true });
15+
}
16+
17+
// Method to display Google One Tap
18+
async displayOneTap(): Promise<void> {
19+
if (this.oneTapInitialized) return;
20+
21+
try {
22+
const resp = await this.sdk.fedcm.oneTap('google');
23+
console.log("One Tap response:", resp);
24+
// Redirect on success
25+
window.location.replace("/dashboard");
26+
this.oneTapInitialized = true;
27+
} catch (error) {
28+
console.error("Failed to display One Tap:", error);
29+
}
30+
}
31+
32+
// Method to check if the user is authenticated
33+
isAuthenticated(): boolean {
34+
const sessionToken = this.sdk.getSessionToken();
35+
if (sessionToken) {
36+
return !this.sdk.isJwtExpired(sessionToken);
37+
}
38+
return false;
39+
}
40+
41+
// Method to check if One Tap has been initialized
42+
isOneTapInitialized(): boolean {
43+
return this.oneTapInitialized;
44+
}
45+
}

0 commit comments

Comments
 (0)