Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Injectable} from '@angular/core';
- import {Http} from '@angular/http';
- import {Api} from './api';
- import {Settings} from './settings';
- import 'rxjs/add/operator/map';
- import 'rxjs/add/operator/toPromise';
- import {Storage} from "@ionic/storage";
- @Injectable()
- export class Auth {
- isLoggedIn: Boolean;
- user_id: String;
- user_token: String;
- constructor(public http: Http, public api: Api, public settings: Settings, public storage: Storage) {
- }
- login(user) {
- this.logout();
- this.storage.set('user_id', user.id);
- this.storage.set('user_jwt', user.jwt);
- this.isLoggedIn = true;
- this.user_id = user._id;
- this.user_token = user.jwt;
- }
- logout() {
- this.storage.remove('user_id');
- this.storage.remove('user_jwt');
- this.isLoggedIn = false;
- this.user_id = null;
- this.user_token = null;
- }
- isAuthenticated() {
- return this.isLoggedIn;
- }
- getUser() {
- console.log("UserID: " + this.user_id);
- console.log("UserToken: " + this.user_token);
- if (this.isAuthenticated()) {
- return {
- id: this.user_id,
- token: this.user_token,
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement