mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 23:27:35 +00:00
added proper error handling for user creation
This commit is contained in:
+72
-68
@@ -2,17 +2,17 @@ import express from 'express';
|
|||||||
import passport from 'passport';
|
import passport from 'passport';
|
||||||
import { AuthData } from '../interfaces/middleware';
|
import { AuthData } from '../interfaces/middleware';
|
||||||
import {
|
import {
|
||||||
AuthProvider,
|
AuthProvider,
|
||||||
User,
|
User,
|
||||||
ServiceAccount,
|
ServiceAccount,
|
||||||
ServiceTokenData,
|
ServiceTokenData,
|
||||||
} from '../models';
|
} from '../models';
|
||||||
import { createToken } from '../helpers/auth';
|
import { createToken } from '../helpers/auth';
|
||||||
import {
|
import {
|
||||||
getClientIdGoogle,
|
getClientIdGoogle,
|
||||||
getClientSecretGoogle,
|
getClientSecretGoogle,
|
||||||
getJwtProviderAuthLifetime,
|
getJwtProviderAuthLifetime,
|
||||||
getJwtProviderAuthSecret
|
getJwtProviderAuthSecret
|
||||||
} from '../config';
|
} from '../config';
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
@@ -26,17 +26,17 @@ const GoogleStrategy = require('passport-google-oauth20').Strategy;
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
const getAuthDataPayloadIdObj = (authData: AuthData) => {
|
const getAuthDataPayloadIdObj = (authData: AuthData) => {
|
||||||
if (authData.authPayload instanceof User) {
|
if (authData.authPayload instanceof User) {
|
||||||
return { userId: authData.authPayload._id };
|
return { userId: authData.authPayload._id };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (authData.authPayload instanceof ServiceAccount) {
|
if (authData.authPayload instanceof ServiceAccount) {
|
||||||
return { serviceAccountId: authData.authPayload._id };
|
return { serviceAccountId: authData.authPayload._id };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (authData.authPayload instanceof ServiceTokenData) {
|
if (authData.authPayload instanceof ServiceTokenData) {
|
||||||
return { serviceTokenDataId: authData.authPayload._id };
|
return { serviceTokenDataId: authData.authPayload._id };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -47,68 +47,72 @@ const getAuthDataPayloadIdObj = (authData: AuthData) => {
|
|||||||
*/
|
*/
|
||||||
const getAuthDataPayloadUserObj = (authData: AuthData) => {
|
const getAuthDataPayloadUserObj = (authData: AuthData) => {
|
||||||
|
|
||||||
if (authData.authPayload instanceof User) {
|
if (authData.authPayload instanceof User) {
|
||||||
return { user: authData.authPayload._id };
|
return { user: authData.authPayload._id };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (authData.authPayload instanceof ServiceAccount) {
|
if (authData.authPayload instanceof ServiceAccount) {
|
||||||
return { user: authData.authPayload.user };
|
return { user: authData.authPayload.user };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (authData.authPayload instanceof ServiceTokenData) {
|
if (authData.authPayload instanceof ServiceTokenData) {
|
||||||
return { user: authData.authPayload.user };
|
return { user: authData.authPayload.user };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const initializePassport = async () => {
|
const initializePassport = async () => {
|
||||||
const googleClientSecret = await getClientSecretGoogle();
|
const googleClientSecret = await getClientSecretGoogle();
|
||||||
const googleClientId = await getClientIdGoogle();
|
const googleClientId = await getClientIdGoogle();
|
||||||
|
|
||||||
passport.use(new GoogleStrategy({
|
passport.use(new GoogleStrategy({
|
||||||
passReqToCallback: true,
|
passReqToCallback: true,
|
||||||
clientID: googleClientId,
|
clientID: googleClientId,
|
||||||
clientSecret: googleClientSecret,
|
clientSecret: googleClientSecret,
|
||||||
callbackURL: '/api/v1/oauth/callback/google',
|
callbackURL: '/api/v1/oauth/callback/google',
|
||||||
scope: ['profile', ' email'],
|
scope: ['profile', ' email'],
|
||||||
}, async (
|
}, async (
|
||||||
req: express.Request,
|
req: express.Request,
|
||||||
accessToken: string,
|
accessToken: string,
|
||||||
refreshToken: string,
|
refreshToken: string,
|
||||||
profile: any,
|
profile: any,
|
||||||
cb: any
|
cb: any
|
||||||
) => {
|
) => {
|
||||||
const email = profile.emails[0].value;
|
try {
|
||||||
let user = await User.findOne({
|
const email = profile.emails[0].value;
|
||||||
|
let user = await User.findOne({
|
||||||
|
authProvider: AuthProvider.GOOGLE,
|
||||||
|
authId: profile.id,
|
||||||
|
}).select('+publicKey')
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
user = await new User({
|
||||||
|
email,
|
||||||
authProvider: AuthProvider.GOOGLE,
|
authProvider: AuthProvider.GOOGLE,
|
||||||
authId: profile.id,
|
authId: profile.id,
|
||||||
}).select('+publicKey')
|
}).save();
|
||||||
|
}
|
||||||
|
|
||||||
if (!user) {
|
const providerAuthToken = createToken({
|
||||||
user = await new User({
|
payload: {
|
||||||
email,
|
userId: user._id.toString(),
|
||||||
authProvider: AuthProvider.GOOGLE,
|
email: user.email,
|
||||||
authId: profile.id,
|
authProvider: user.authProvider,
|
||||||
}).save();
|
isUserCompleted: !!user.publicKey
|
||||||
}
|
},
|
||||||
|
expiresIn: await getJwtProviderAuthLifetime(),
|
||||||
|
secret: await getJwtProviderAuthSecret(),
|
||||||
|
});
|
||||||
|
|
||||||
const providerAuthToken = createToken({
|
req.providerAuthToken = providerAuthToken;
|
||||||
payload: {
|
cb(null, profile);
|
||||||
userId: user._id.toString(),
|
} catch (err) {
|
||||||
email: user.email,
|
cb(err);
|
||||||
authProvider: user.authProvider,
|
}
|
||||||
isUserCompleted: !!user.publicKey
|
}));
|
||||||
},
|
|
||||||
expiresIn: await getJwtProviderAuthLifetime(),
|
|
||||||
secret: await getJwtProviderAuthSecret(),
|
|
||||||
});
|
|
||||||
|
|
||||||
req.providerAuthToken = providerAuthToken;
|
|
||||||
cb(null, profile);
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
getAuthDataPayloadIdObj,
|
getAuthDataPayloadIdObj,
|
||||||
getAuthDataPayloadUserObj,
|
getAuthDataPayloadUserObj,
|
||||||
initializePassport,
|
initializePassport,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user