这是到应用程序的链接。 App
问题:当我尝试转到用户个人资料页面时,它很好地重定向到登录页面,然后返回到主页,这不是它的预定路线。
思想:找出原因(至少我认为)在登录功能中,因为每次用户登录时都会将其重定向到首页。因此尝试进行调整,但徒劳无功。
这是用户服务
export class UserService() {
private addrr = '/api';
return: string = '';
token: string;
constructor(private http: Http, private router: Router, private route: ActivatedRoute) { }
ngOnInit() {
// Get the query params
this.route.queryParams
.subscribe(params => this.return = params['return'] || '/home');
}
// get("/api/users/userReg")
getUser(): Promise<void | User[]> {
return this.http.get(this.addrr + '/users/userReg')
.toPromise()
.then(response => response.json() as User[])
.catch(this.handleError);
}
// post("/api/users/register")
createUser(newUser: User): Promise<void | User> {
return this.http.post(this.addrr + '/users/register', newUser)
.toPromise()
.then(response => response.json() as User)
.then(() => {
this.router.navigate(['/users/login']);
})
.catch(this.handleError);
}
//login
login(authCridentials: User): Promise<void | User> {
return this.http.post(this.addrr + '/users/authenticate', authCridentials)
.toPromise()
.then(response => response.json() as User)
.then((respond) => {
this.setToken(respond['token']);
// this.router.navigate(['/home']);
this.router.navigate([this.return]);
})
.catch(this.handleError);
}
logout() {
this.token = null;
localStorage.clear();
}
setToken(token) {
localStorage.setItem('token', token);
}
deleteToke() {
localStorage.removeItem('token');
}
getUserPayLoad() {
var token = localStorage.getItem('token');
if (token) {
var UserPayLoad = atob(token.split('.')[1]);
return JSON.parse(UserPayLoad)
} else {
return null;
}
}
isLoggedIn() {
var UserPayLoad = this.getUserPayLoad();
if (UserPayLoad) {
return UserPayLoad.exp > Date.now() / 1000;
} else {
return false;
}
}
}
this is app routung
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from './auth/auth.guard';
import { UserProfileComponent } from './user-profile/user-profile.component';
const routes: Routes = [
{
path: 'users',
loadChildren: './users/users.module#UsersModule'
},
{
path: 'home',
loadChildren: './home/home.module#HomeModule'
},
{
path: 'admin',
canLoad: [AuthGuard],
loadChildren: './admin/admin.module#AdminModule'
},
{
path: 'userprofile',
component: UserProfileComponent,
canActivate: [AuthGuard]
},
{
path: 'home',
redirectTo: '',
pathMatch: 'full'
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
providers: [AuthGuard],
exports: [RouterModule]
})
export class AppRoutingModule { }
this is authguard
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad } from '@angular/router';
import { Observable } from 'rxjs';
import { UserService } from '../shared/user.service';
import { Router, Route } from "@angular/router";
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanLoad {
constructor(public userService: UserService, private router: Router) { }
canLoad(route: Route): boolean {
if (this.userService.isLoggedIn()) {
this.router.navigate(['/admin/list']);
return true;
}
this.router.navigate(['/users/login']);
return false;
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.userService.isLoggedIn()) {
return true;
}
this.router.navigate(['/users/login']);
}
}
this is link in navbar
routerLink="/userprofile"