Как делать редирект на страницу логина, если пользователь пытается сделать переход на другие ссылки?
А точнее, как правильно сделать проверку на то, залогинен ли пользователь?
UserService
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MongoService } from 'wacom';
import { tokenNotExpired } from 'angular2-jwt'
export interface User {
name: string;
_id: string;
data: object;
}
@Injectable({
providedIn: 'root'
})
export class UserService implements User {
/*
* Declarations
*/
public users: User[];
public name;
public email;
public avatarUrl;
public _users: any = {};
public _id;
public is: any = {};
public data: any = {
description: String,
resetPin: {},
resetCreate: Date,
resetCounter: Number,
request: {},
reason: {},
phone: Number,
location: String,
skills: {},
tools: {},
balance: {},
};
/*
* Code
*/
constructor(private mongo: MongoService, private http: HttpClient) {
http.get('/api/user/me').subscribe((resp:any) => {
if(typeof resp.data != 'object') resp.data = {};
if(typeof resp.is != 'object') resp.is = {};
if(typeof resp.data.balance != 'object') resp.data.balance = {};
if(typeof resp.data.request != 'object') resp.data.request = {};
if(typeof resp.data.reason !='object') resp.data.reason = {};
if(typeof resp.data.skills != 'object') resp.data.skills = {};
if(typeof resp.data.tools !='object') resp.data.tools = {};
// if(!Array.isArray(this.data.friends)) this.data.friends = [];
if(typeof resp.data.balance != 'object') resp.data.balance = {};
for (var key in resp) {
this[key] = resp[key];
}
this.users = mongo.get('user', {
replace: {
data: (field, cb, doc) => {
if(typeof field != 'object') field = {};
if(typeof field.request!='object') field.request = {};
if(typeof field.reason !='object') field.reason = {};
if(typeof field.skills!='object') field.skills = {};
if(typeof field.tools !='object') field.tools = {};
},
is: (field, cb) => {
cb(field || {})
}
}
}, (arr, obj) => {
this._users = obj;
});
});
}
update(){
this.mongo.afterWhile(this, ()=>{
this.mongo.update('user', this, {
fields: 'name data avatarUrl'
});
});
}
save(user){
this.mongo.afterWhile(this, ()=>{
this.mongo.updateAll('user', user, {
name: 'admin'
});
});
}
change_password(oldPass, newPass){
this.http.post('/api/user/changePassword', {
newPass: newPass,
oldPass: oldPass
}).subscribe(resp => {
if(resp) alert('successfully changed password');
else alert('failed to change password');
});
}
remove_role(role){
this.mongo.updateAll('user', {
remove: role,
data: this.data,
name: this.name
});
}
todataUrl(fl, cb) {
var a = new FileReader();
a.onload = (e)=>{
var target: any = e.target;
cb(target.result);
}
a.readAsDataURL(fl);
}
changeAvatar(e){
this.todataUrl(e.target.files[0], (dataUrl)=>{
this.http.post('/api/user/avatar', {
dataUrl: dataUrl
}).subscribe((resp:any)=> {
console.log(resp);
this.avatarUrl = resp;
});
});
}
create(user) {
this.mongo.create('user', user, created=>{
console.log(created);
});
}
// isLogged() {
// return tokenNotExpired
// }
}
Guard
import { UserService } from '@services';
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, Route } from '@angular/router';
import { Observable } from 'rxjs';
@Injectable()
export class IsLoggedIn implements CanActivate {
public user: any = {}
constructor(
private us: UserService,
private router: Router,
private mongo: MongoService
) {
}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (!this.user.email) {
this.router.navigate(['/sign']);
} else {
return true;
}
return false;
}
}