身份验证后如何将用户重定向到原始请求,Angular-Fullstack Yeoman? [英] How to redirect users to original request after authentication, Angular-Fullstack Yeoman?
问题描述
我正在使用Angular-Fullstack yeoman生成器作为我的项目的基础: https://github.com/DaftMonk/generator-angular-fullstack
I'm using the Angular-Fullstack yeoman generator as the basis for my project: https://github.com/DaftMonk/generator-angular-fullstack
我无法弄清楚如何在登录后将用户重定向到他们最初请求的链接.
I cannot figure out how to redirect users to the link they originally requested after logging in.
我想发生的事的例子:
- 未经身份验证的用户请求 http://myapp/videos/video1
- 用户被引导登录
- 用户成功进行身份验证
- 用户自动重定向到 http://myapp/videos/video1
- Unauthenticated user requests http://myapp/videos/video1
- User is directed to log in
- User successfully authenticates
- User is automatically redirected to http://myapp/videos/video1
我同时使用样板本地登录名和样板OAuth.
I am using both the boilerplate local login and boilerplate OAuth.
感谢您的帮助!
推荐答案
我已经弄清楚了,这是我解决该问题所采取的步骤. 由于某种原因,堆栈溢出未格式化下面的最后两个代码块.我对下面的代码进行了总结(请注意,需要修改3个单独的文件)
https://gist.github.com/dcoffey3296/d27c141ef79bec3ff6a6
I figured it out, here are the steps I took to solve this problem. For some reason, Stack Overflow isn't formatting my last 2 code blocks below. I made a gist with the code below (note 3 separate files need to be modified)
https://gist.github.com/dcoffey3296/d27c141ef79bec3ff6a6
-
将要返回的网址存储在
client/app/app.js
.run(function ($rootScope, $location, Auth, $cookieStore) {
// Redirect to login if route requires auth and you're not logged in
$rootScope.$on('$stateChangeStart', function (event, next) {
Auth.isLoggedInAsync(function(loggedIn) {
if (next.authenticate && !loggedIn) {
// store the requested url if not logged in
if ($location.url() != '/login')
{
$cookieStore.put('returnUrl', $location.url());
}
$location.path('/login');
}
});
});
});
对于Oauth,请检查此Cookie并重定向到server/auth/auth.service.js
function setTokenCookie(req, res) {
if (!req.user) {
return res.json(404, { message: 'Something went wrong, please try again.'});
}
var token = signToken(req.user._id, req.user.role);
res.cookie('token', JSON.stringify(token));
// return the user to the request page (oAuth) or homepage
if (typeof req.cookies.returnUrl != 'undefined')
{
res.redirect(req.cookies.returnUrl.replace(/"/g, "") || '/');
}
else
{
res.redirect('/');
}
}
对于本地登录,请检查$scope.login()
的.then()
部分中的cookie,文件:client/app/account/login/login.controller.js
for local login, check for cookie in the .then()
part of $scope.login()
, file: client/app/account/login/login.controller.js
.then( function() {
// Logged in, redirect to home
if (typeof $cookieStore.get('returnUrl') != 'undefined' && $cookieStore.get('returnUrl') != '')
{
$location.path($cookieStore.get('returnUrl'));
$cookieStore.remove('returnUrl');
}
else
{
$location.path('/');
}
})
这篇关于身份验证后如何将用户重定向到原始请求,Angular-Fullstack Yeoman?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!