Installation npm i vi-auth
Example usage for sign in:
const authFactory = require('vi-auth').default;
const {validateUserCanLogin} = require('../../modules/user');
const {jwt} = require('./path-to-config.js');
const {RefreshToken} = require('../../../../models.js');
const UserAuthFunc = () => {
return authFactory(function ({email, password}) {
//todo add code here that validate is user with this password and email can login, and return the user id
}, {
secret: '!23*T2jFI0nSiVg4Tr6uVM',
accessTokenTTL: "1h",
refreshTokenTTL: "10d",
}, null, null, RefreshToken)
};
const signIn = (context, input) => {
return UserAuthFunc()
.handleAccessTokenRequest(input)
.then(({userIdentifier,...authResponse})
};
const signOut = (context) => UserAuthFunc().handleRevokeTokenRequest(context.get('req'))
.then(() => true);
const refreshAccessToken = (context, {refreshToken}) => UserAuthFunc().handleRefreshTokenRequest({refreshToken});
Example usage for auth middleware:
if (req.headers && req.headers['authorization']) {
const accessTokenEntity = await UserAuthFunc().validateAuthenticatedRequest(req);
const user = await UserModule.getUser(accessTokenEntity.getUserIdentifier());
}