|
|
@ -156,57 +156,93 @@ export async function signup(req: Request, res: Response<TableType>) { |
|
|
|
} as any); |
|
|
|
} as any); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function successfulSignIn({ |
|
|
|
|
|
|
|
user, |
|
|
|
|
|
|
|
err, |
|
|
|
|
|
|
|
info, |
|
|
|
|
|
|
|
req, |
|
|
|
|
|
|
|
res, |
|
|
|
|
|
|
|
auditDescription |
|
|
|
|
|
|
|
}) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (!user || !user.email) { |
|
|
|
|
|
|
|
if (err) { |
|
|
|
|
|
|
|
return res.status(400).send(err); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (info) { |
|
|
|
|
|
|
|
return res.status(400).send(info); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return res.status(400).send({ msg: 'Your signin has failed' }); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await promisify((req as any).login.bind(req))(user); |
|
|
|
|
|
|
|
const refreshToken = randomTokenString(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await User.update(user.id, { |
|
|
|
|
|
|
|
refresh_token: refreshToken |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
setTokenCookie(res, refreshToken); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Audit.insert({ |
|
|
|
|
|
|
|
op_type: 'AUTHENTICATION', |
|
|
|
|
|
|
|
op_sub_type: 'SIGNIN', |
|
|
|
|
|
|
|
user: user.email, |
|
|
|
|
|
|
|
ip: req.clientIp, |
|
|
|
|
|
|
|
description: auditDescription |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.json({ |
|
|
|
|
|
|
|
token: jwt.sign( |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
email: user.email, |
|
|
|
|
|
|
|
firstname: user.firstname, |
|
|
|
|
|
|
|
lastname: user.lastname, |
|
|
|
|
|
|
|
id: user.id, |
|
|
|
|
|
|
|
roles: user.roles |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Noco.getConfig().auth.jwt.secret, |
|
|
|
|
|
|
|
Noco.getConfig().auth.jwt.options |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} as any); |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
console.log(e); |
|
|
|
|
|
|
|
throw e; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function signin(req, res, next) { |
|
|
|
async function signin(req, res, next) { |
|
|
|
passport.authenticate( |
|
|
|
passport.authenticate( |
|
|
|
'local', |
|
|
|
'local', |
|
|
|
{ session: false }, |
|
|
|
{ session: false }, |
|
|
|
async (err, user, info): Promise<any> => { |
|
|
|
async (err, user, info): Promise<any> => |
|
|
|
try { |
|
|
|
await successfulSignIn({ |
|
|
|
if (!user || !user.email) { |
|
|
|
user, |
|
|
|
if (err) { |
|
|
|
err, |
|
|
|
return res.status(400).send(err); |
|
|
|
info, |
|
|
|
} |
|
|
|
req, |
|
|
|
if (info) { |
|
|
|
res, |
|
|
|
return res.status(400).send(info); |
|
|
|
auditDescription: 'signed in' |
|
|
|
} |
|
|
|
}) |
|
|
|
return res.status(400).send({ msg: 'Your signin has failed' }); |
|
|
|
)(req, res, next); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await promisify((req as any).login.bind(req))(user); |
|
|
|
async function googleSignin(req, res, next) { |
|
|
|
const refreshToken = randomTokenString(); |
|
|
|
passport.authenticate( |
|
|
|
|
|
|
|
'google', |
|
|
|
await User.update(user.id, { |
|
|
|
{ |
|
|
|
refresh_token: refreshToken |
|
|
|
session: false, |
|
|
|
}); |
|
|
|
callbackURL: req.ncSiteUrl + Noco.getConfig().dashboardPath |
|
|
|
setTokenCookie(res, refreshToken); |
|
|
|
}, |
|
|
|
|
|
|
|
async (err, user, info): Promise<any> => |
|
|
|
Audit.insert({ |
|
|
|
await successfulSignIn({ |
|
|
|
op_type: 'AUTHENTICATION', |
|
|
|
user, |
|
|
|
op_sub_type: 'SIGNIN', |
|
|
|
err, |
|
|
|
user: user.email, |
|
|
|
info, |
|
|
|
ip: req.clientIp, |
|
|
|
req, |
|
|
|
description: `signed in` |
|
|
|
res, |
|
|
|
}); |
|
|
|
auditDescription: 'signed in using Google Auth' |
|
|
|
|
|
|
|
}) |
|
|
|
res.json({ |
|
|
|
|
|
|
|
token: jwt.sign( |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
email: user.email, |
|
|
|
|
|
|
|
firstname: user.firstname, |
|
|
|
|
|
|
|
lastname: user.lastname, |
|
|
|
|
|
|
|
id: user.id, |
|
|
|
|
|
|
|
roles: user.roles |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Noco.getConfig().auth.jwt.secret, |
|
|
|
|
|
|
|
Noco.getConfig().auth.jwt.options |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} as any); |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
console.log(e); |
|
|
|
|
|
|
|
throw e; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
)(req, res, next); |
|
|
|
)(req, res, next); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -461,6 +497,18 @@ const mapRoutes = router => { |
|
|
|
); |
|
|
|
); |
|
|
|
router.post('/auth/token/refresh', ncMetaAclMw(refreshToken, 'refreshToken')); |
|
|
|
router.post('/auth/token/refresh', ncMetaAclMw(refreshToken, 'refreshToken')); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Google auth apis */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router.post(`/auth/google/genTokenByCode`, catchError(googleSignin)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router.get('/auth/google', (req: any, res, next) => |
|
|
|
|
|
|
|
passport.authenticate('google', { |
|
|
|
|
|
|
|
scope: ['profile', 'email'], |
|
|
|
|
|
|
|
state: req.query.state, |
|
|
|
|
|
|
|
callbackURL: req.ncSiteUrl + Noco.getConfig().dashboardPath |
|
|
|
|
|
|
|
})(req, res, next) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// new API
|
|
|
|
// new API
|
|
|
|
router.post('/api/v1/db/auth/user/signup', catchError(signup)); |
|
|
|
router.post('/api/v1/db/auth/user/signup', catchError(signup)); |
|
|
|
router.post('/api/v1/db/auth/user/signin', catchError(signin)); |
|
|
|
router.post('/api/v1/db/auth/user/signin', catchError(signin)); |
|
|
|