Finished local user login flow
This commit is contained in:
@ -44,33 +44,63 @@ module.exports = function(User) {
|
|||||||
|
|
||||||
User.beforeRemote('login', function(ctx, instance, next) {
|
User.beforeRemote('login', function(ctx, instance, next) {
|
||||||
debug('before called');
|
debug('before called');
|
||||||
debug(ctx, instance, next);
|
|
||||||
|
//debug(ctx, instance, next);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
User.afterRemote('confirm', function(ctx, instance, next) {
|
||||||
|
ctx.req.flash('success', {
|
||||||
|
msg: [
|
||||||
|
'You\'re email has been confirmed!'
|
||||||
|
]
|
||||||
|
});
|
||||||
|
ctx.res.redirect('/email-signin');
|
||||||
|
});
|
||||||
|
|
||||||
User.afterRemote('login', function(ctx, instance, next) {
|
User.afterRemote('login', function(ctx, instance, next) {
|
||||||
|
debug('after called');
|
||||||
var res = ctx.res;
|
var res = ctx.res;
|
||||||
var req = ctx.req;
|
var req = ctx.req;
|
||||||
|
|
||||||
if (!instance || instance.emailVerified !== true) {
|
User.findOne({where: {email: ctx.args.credentials.email}},
|
||||||
debug(instance);
|
function(err, response) {
|
||||||
req.flash('errors', {
|
if (err) {
|
||||||
msg: [
|
return next(err);
|
||||||
'Please verify your email address.'
|
}
|
||||||
]
|
if (response.emailVerified !== true) {
|
||||||
|
return res.redirect('/');
|
||||||
|
}
|
||||||
|
User.login({
|
||||||
|
email: ctx.args.credentials.email,
|
||||||
|
password: ctx.args.credentials.password,
|
||||||
|
ttl: Infinity
|
||||||
|
}, function(err, accessToken) {
|
||||||
|
if (err) {
|
||||||
|
req.flash('errors', {
|
||||||
|
msg: [
|
||||||
|
'Invalid username or password.'
|
||||||
|
]
|
||||||
|
});
|
||||||
|
return res.redirect('/');
|
||||||
|
}
|
||||||
|
var config = {
|
||||||
|
signed: !!req.signedCookies,
|
||||||
|
maxAge: accessToken.ttl
|
||||||
|
};
|
||||||
|
if (accessToken && accessToken.id) {
|
||||||
|
res.cookie('access_token', accessToken.id, config);
|
||||||
|
res.cookie('userId', accessToken.userId, config);
|
||||||
|
}
|
||||||
|
req.logIn(response, function(err) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
req.flash('success', { msg: 'Success! You are logged in.' });
|
||||||
|
return res.redirect('/');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
return res.redirect('/');
|
|
||||||
}
|
|
||||||
|
|
||||||
var config = {
|
|
||||||
signed: !!req.signedCookies,
|
|
||||||
maxAge: 1000 * accessToken.ttl
|
|
||||||
};
|
|
||||||
if (accessToken && accessToken.id) {
|
|
||||||
res.cookie('access_token', accessToken.id, config);
|
|
||||||
res.cookie('userId', accessToken.userId, config);
|
|
||||||
}
|
|
||||||
res.redirect('/');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user