-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d625a9
commit d64c34d
Showing
9 changed files
with
81 additions
and
116 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as process from 'process' | ||
|
||
const API_URL = process.env.API_URL || 'http://localhost:3000' | ||
|
||
export { API_URL } |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,32 @@ | ||
import { Injectable } from '@angular/core' | ||
import { HttpClient, HttpErrorResponse } from '@angular/common/http' | ||
import { Observable, of, throwError } from 'rxjs' | ||
import { catchError, map } from 'rxjs/operators' | ||
import { Keypair } from '@stellar/stellar-sdk' | ||
import { API_URL } from '../config/config' | ||
import { HttpClient } from '@angular/common/http' | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AuthService { | ||
constructor(private http: HttpClient) {} | ||
|
||
generateKeyPair(): Keypair { | ||
return Keypair.random() | ||
} | ||
private apiURL = API_URL | ||
|
||
register(publicKey: string): Observable<any> { | ||
return this.http | ||
.post('/api/users/register', { publicKey }) | ||
.pipe(catchError(this.handleError)) | ||
} | ||
|
||
login(publicKey: string): Observable<any> { | ||
return this.http | ||
.post('/api/auth/login', { publicKey }, { withCredentials: true }) | ||
.pipe(catchError(this.handleError)) | ||
} | ||
constructor(private http: HttpClient) {} | ||
|
||
isAuthenticated(): Observable<boolean> { | ||
return this.http.get('/api/auth/check', { withCredentials: true }).pipe( | ||
catchError(() => of({ authenticated: false })), | ||
map((response: any) => response.authenticated), | ||
) | ||
getChallenge(publicKey: string) { | ||
return this.http.get<any>(`${this.apiURL}/auth/challenge}`, { | ||
params: { publicKey }, | ||
}) | ||
} | ||
|
||
private handleError(error: HttpErrorResponse) { | ||
console.error('An error occurred:', error.message) | ||
return throwError( | ||
() => new Error('Something went wrong. Please try again later.'), | ||
) | ||
authenticate( | ||
transaction: string, | ||
publicKey: string, | ||
captchaId: string, | ||
captchaAnswer: string, | ||
) { | ||
return this.http.post<any>(`${this.apiURL}/auth/authenticate`, { | ||
transaction, | ||
publicKey: publicKey, | ||
captchaId, | ||
captchaAnswer, | ||
}) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,20 @@ | ||
import { Injectable } from '@angular/core' | ||
import { CanActivate, Router } from '@angular/router' | ||
import { Observable, of } from 'rxjs' | ||
import { catchError, map } from 'rxjs/operators' | ||
import { AuthService } from '../core/auth.service' | ||
import { | ||
ActivatedRouteSnapshot, | ||
CanActivate, | ||
GuardResult, | ||
MaybeAsync, | ||
RouterStateSnapshot, | ||
} from '@angular/router' | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AuthGuard implements CanActivate { | ||
constructor(private authService: AuthService, private router: Router) {} | ||
|
||
canActivate(): Observable<boolean> { | ||
return this.authService.isAuthenticated().pipe( | ||
map(authenticated => { | ||
if (!authenticated) { | ||
this.router.navigate(['/login']) | ||
return false | ||
} | ||
return true | ||
}), | ||
catchError(() => { | ||
this.router.navigate(['/login']) | ||
return of(false) | ||
}), | ||
) | ||
canActivate( | ||
route: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot, | ||
): MaybeAsync<GuardResult> { | ||
return false | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.