Last active
December 12, 2016 22:22
-
-
Save zevisert/4cbbdf8e473bd7d8894816cf33c4df4a to your computer and use it in GitHub Desktop.
Auth Service
This file contains hidden or 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
@Injectable() | |
export class AuthService { | |
... | |
validate(pattern: string, route: string): void { | |
let body: AuthBody = { type: "Anonymous", payload: pattern }; | |
// Fire off a request to the server to see if the pattern entered is valid | |
this.postAuth(body) | |
.then((success: boolean) => { | |
if (success) { | |
// Result was good, close the modal | |
this.doneAuth$.next(); | |
// And move to wherever we were tring to go | |
this.router.navigateByUrl(route); | |
} else { | |
// Pattern was rejected | |
this.errorAuth$.next("pattern"); | |
} | |
}) | |
.catch(this.handleError); | |
} | |
private postAuth(body: AuthBody): Promise<boolean> { | |
// Send the request and payload | |
return this.http.post(this.authURL, body) | |
.toPromise() | |
.then((res: Response) => { | |
if (res.json().success as boolean) { | |
// On success save the token | |
localStorage.setItem(this.tokenName, res.json().token); | |
} | |
// Report the success or failure of the request | |
return res.json().success as boolean; | |
}) | |
.catch(this.handleError); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment