gpt4 book ai didi

javascript - Angular SSR 网站在转到实际路径之前重定向到登录页面

转载 作者:行者123 更新时间:2023-12-03 18:54:05 25 4
gpt4 key购买 nike

我使用 Angular SSR,当我刷新页面时,它首先进入登录页面,然后进入所需页面。
我还使用 cookie 来存储 token 。

canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.tokenService.isAuthenticated$.pipe(
map(isAuth => {
if (!isAuth) {
this.router.navigate(['auth']).then();
return false;
}
return true;
})
);
}
const routes: Routes =  [
{
path: 'auth',
canActivate: [AuthGuard],
loadChildren: () => import('./main/auth/auth.module').then(m => m.AuthModule)
},
{
path: '', resolve: {mainInfo: MainResolverService}, component: MainComponent, children: [
{path: '', redirectTo: 'home', pathMatch: 'full'},
{path: 'home', loadChildren: () => import('./main/modules/global/home/home.module').then(m => m.HomeModule)},
{path: 'faq', loadChildren: () => import('./main/modules/global/faq/faq.module').then(m => m.FaqModule)},
{path: 'cooperation-request', loadChildren: () => import('./main/modules/global/cooperation-request/cooperation-request-module').then(m => m.CooperationRequestModule)},
{path: 'contact-us', loadChildren: () => import('./main/modules/global/contact-us/contact-us.module').then(m => m.ContactUsModule)},
{path: 'mentors', loadChildren: () => import('./main/modules/global/mentors/mentors.module').then(m => m.MentorsModule)},
{path: 'category', loadChildren: () => import('./main/modules/global/category/category.module').then(m => m.CategoryModule)},
{path: 'about', loadChildren: () => import('./main/modules/global/about/about.module').then(m => m.AboutModule)},
{path: 'webinar/:id/:title', loadChildren: () => import('./main/modules/global/webinar/webinar.module').then(m => m.WebinarModule)},
{path: 'user', canActivate: [AuthenticatedGuard], loadChildren: () => import('./main/modules/secured/user.module').then(m => m.UserModule)},
]
}
];

最佳答案

这是因为API是在浏览器上调用的,而API必须在Express js中调用。
您可以通过 cookie-parser 包调用 Express 中的 cookie。

var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();
app.use(cookieParser());

关于javascript - Angular SSR 网站在转到实际路径之前重定向到登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66291532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com