-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finalize login dialog and add a token service
- Loading branch information
Showing
3 changed files
with
37 additions
and
1 deletion.
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
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,25 @@ | ||
export default class TokenService { | ||
private tokenKey: string = "token"; | ||
|
||
public setToken(token: string) { | ||
localStorage.setItem(this.tokenKey, token); | ||
} | ||
|
||
public getToken(): string { | ||
return localStorage.getItem(this.tokenKey) ?? ""; | ||
} | ||
|
||
public isLoggedIn(): boolean { | ||
// Won't work if the token is expired | ||
return this.getToken() !== ""; | ||
} | ||
|
||
public getUserName() { | ||
const token = this.getToken(); | ||
if (token === "") { | ||
return ""; | ||
} | ||
console.log(JSON.parse(atob(token.split(".")[1]))); | ||
return JSON.parse(atob(token.split(".")[1])).userName; | ||
} | ||
} |