Guest User

Untitled

a guest
Jul 12th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. import { gun } from './gun.service';
  2. export const STATUS_SUCCESS = 'success';
  3. export const STATUS_ERROR = 'error';
  4. const ACTION_CREATE = 'create';
  5. const ACTION_AUTH = 'auth';
  6. const SESSION_KEY_USERNAME = 'rt_username';
  7. const SESSION_KEY_PASSWORD = 'rt_password';
  8.  
  9. export interface Credentials {
  10. username: string;
  11. password: string;
  12. }
  13.  
  14. export interface FriendlyResponse {
  15. status: string;
  16. message: string;
  17. data?: any;
  18. }
  19.  
  20. class AuthService {
  21. private referer: string = '';
  22.  
  23. public async isAuthenticated(): Promise<FriendlyResponse> {
  24. const credentials: Credentials|null = this.getCredentials();
  25. const errorResponse: FriendlyResponse = { status: STATUS_ERROR, message: 'please login'};
  26. if (!credentials) {
  27. return errorResponse;
  28. }
  29. try {
  30. const response = await this.login(credentials);
  31. return response;
  32. } catch (e) {
  33. this.unsetCredentials();
  34. }
  35. return errorResponse;
  36. }
  37.  
  38. public login(credentials: Credentials): Promise<FriendlyResponse> {
  39. return this.auth(credentials, ACTION_AUTH, 'loggedin');
  40. }
  41.  
  42. public register(credentials: Credentials): Promise<FriendlyResponse> {
  43. return this.auth(credentials, ACTION_CREATE, 'registered');
  44. }
  45.  
  46. private auth(credentials: Credentials, action: string, message: string): Promise<FriendlyResponse> {
  47. return new Promise((resolve, reject) => {
  48. gun.user()[action](credentials.username, credentials.password, (ack) => {
  49. if (ack.err || ack.status === 'error') {
  50. reject({ status: STATUS_ERROR, message: ack.err || ack.message});
  51. } else {
  52. this.setCredentials(credentials);
  53. resolve({ status: STATUS_SUCCESS, message });
  54. }
  55. });
  56. });
  57. }
  58.  
  59. public async logout() {
  60. this.unsetCredentials();
  61. gun.user().leave();
  62. }
  63.  
  64. private getCredentials(): Credentials|null {
  65. const username: string = sessionStorage.getItem(SESSION_KEY_USERNAME) || '';
  66. const password: string = sessionStorage.getItem(SESSION_KEY_PASSWORD) || '';
  67. if (!username || !password) {
  68. return null;
  69. }
  70. return { username, password };
  71. }
  72.  
  73. private setCredentials(credentials: Credentials) {
  74. sessionStorage.setItem(SESSION_KEY_USERNAME, credentials.username);
  75. sessionStorage.setItem(SESSION_KEY_PASSWORD, credentials.password);
  76. }
  77.  
  78. private unsetCredentials() {
  79. sessionStorage.removeItem(SESSION_KEY_USERNAME);
  80. sessionStorage.removeItem(SESSION_KEY_PASSWORD);
  81. }
  82.  
  83. public setReferrer(url: string) {
  84. this.referer = url;
  85. }
  86.  
  87. public getReferer(): string {
  88. return this.referer;
  89. }
  90. }
  91.  
  92. export default new AuthService();
Add Comment
Please, Sign In to add comment