|
|
@@ -5,10 +5,11 @@ import { Observable } from 'rxjs';
|
|
|
import { AuthResponse, LoginPayload, RegisterPayload } from '../interfaces/interface';
|
|
|
import { AuthenticationResponseJSON, RegistrationResponseJSON, startAuthentication, startRegistration } from '@simplewebauthn/browser';
|
|
|
import { A11yModule } from '@angular/cdk/a11y';
|
|
|
+import { webConfig } from '../config';
|
|
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
|
export class AuthService {
|
|
|
- private readonly baseUrl = 'https://b8c8-115-132-229-66.ngrok-free.app/api';
|
|
|
+ private readonly baseUrl = webConfig.exposedUrl + `/api`
|
|
|
private readonly tokenKey = 'auth_token';
|
|
|
private userName!: string
|
|
|
|
|
|
@@ -108,6 +109,40 @@ export class AuthService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ // login via differnt devices.
|
|
|
+ async webauthnPasskeyLogin(): Promise<AuthResponse> {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ try {
|
|
|
+ // 1. Request generic passkey login options from backend
|
|
|
+ const options = await this.http
|
|
|
+ .post<any>(`${this.baseUrl}/auth/webauthn-login-options`, { passkey: true })
|
|
|
+ .toPromise();
|
|
|
+
|
|
|
+ if (!options) throw new Error('Login options not received');
|
|
|
+
|
|
|
+ // 2. Start WebAuthn login
|
|
|
+ const assertionResponse: AuthenticationResponseJSON = await startAuthentication({
|
|
|
+ optionsJSON: options,
|
|
|
+ });
|
|
|
+
|
|
|
+ // 3. Send only the credential back — backend must resolve user from credential ID
|
|
|
+ const res = await this.http
|
|
|
+ .post<AuthResponse>(`${this.baseUrl}/auth/webauthn-login`, assertionResponse)
|
|
|
+ .toPromise();
|
|
|
+
|
|
|
+ if (!res?.access_token) {
|
|
|
+ reject(new Error('Invalid login response'));
|
|
|
+ } else {
|
|
|
+ this.storeToken(res.access_token);
|
|
|
+ this.setUserName(res.name);
|
|
|
+ resolve(res);
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ reject(err);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
reportAttendance(url: string): Observable<AuthResponse> {
|
|
|
let payload = {
|
|
|
name: this.userName,
|
|
|
@@ -151,7 +186,7 @@ export class AuthService {
|
|
|
|
|
|
logout(): void {
|
|
|
localStorage.removeItem(this.tokenKey);
|
|
|
- this.router.navigate(['/login']);
|
|
|
+ this.router.navigate(['/webauthn-login']);
|
|
|
}
|
|
|
|
|
|
getServerUrl(): Observable<string> {
|