Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

added proper error handling for user creation

Sheen Capadngan4b2e91da7482
diff --git a/backend/src/utils/auth.ts b/backend/src/utils/auth.ts
index e710561e8b..511c20d650 100644
--- a/backend/src/utils/auth.ts
+++ b/backend/src/utils/auth.ts
@@ -2,17 +2,17 @@ import express from 'express';
import passport from 'passport';
import { AuthData } from '../interfaces/middleware';
import {
- AuthProvider,
- User,
- ServiceAccount,
- ServiceTokenData,
+ AuthProvider,
+ User,
+ ServiceAccount,
+ ServiceTokenData,
} from '../models';
import { createToken } from '../helpers/auth';
import {
- getClientIdGoogle,
- getClientSecretGoogle,
- getJwtProviderAuthLifetime,
- getJwtProviderAuthSecret
+ getClientIdGoogle,
+ getClientSecretGoogle,
+ getJwtProviderAuthLifetime,
+ getJwtProviderAuthSecret
} from '../config';
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -26,17 +26,17 @@ const GoogleStrategy = require('passport-google-oauth20').Strategy;
* @returns
*/
const getAuthDataPayloadIdObj = (authData: AuthData) => {
- if (authData.authPayload instanceof User) {
- return { userId: authData.authPayload._id };
- }
+ if (authData.authPayload instanceof User) {
+ return { userId: authData.authPayload._id };
+ }
- if (authData.authPayload instanceof ServiceAccount) {
- return { serviceAccountId: authData.authPayload._id };
- }
+ if (authData.authPayload instanceof ServiceAccount) {
+ return { serviceAccountId: authData.authPayload._id };
+ }
- if (authData.authPayload instanceof ServiceTokenData) {
- return { serviceTokenDataId: authData.authPayload._id };
- }
+ if (authData.authPayload instanceof ServiceTokenData) {
+ return { serviceTokenDataId: authData.authPayload._id };
+ }
};
@@ -47,68 +47,72 @@ const getAuthDataPayloadIdObj = (authData: AuthData) => {
*/
const getAuthDataPayloadUserObj = (authData: AuthData) => {
- if (authData.authPayload instanceof User) {
- return { user: authData.authPayload._id };
- }
+ if (authData.authPayload instanceof User) {
+ return { user: authData.authPayload._id };
+ }
- if (authData.authPayload instanceof ServiceAccount) {
- return { user: authData.authPayload.user };
- }
+ if (authData.authPayload instanceof ServiceAccount) {
+ return { user: authData.authPayload.user };
+ }
- if (authData.authPayload instanceof ServiceTokenData) {
- return { user: authData.authPayload.user };
- }
+ if (authData.authPayload instanceof ServiceTokenData) {
+ return { user: authData.authPayload.user };
+ }
}
const initializePassport = async () => {
- const googleClientSecret = await getClientSecretGoogle();
- const googleClientId = await getClientIdGoogle();
-
- passport.use(new GoogleStrategy({
- passReqToCallback: true,
- clientID: googleClientId,
- clientSecret: googleClientSecret,
- callbackURL: '/api/v1/oauth/callback/google',
- scope: ['profile', ' email'],
- }, async (
- req: express.Request,
- accessToken: string,
- refreshToken: string,
- profile: any,
- cb: any
- ) => {
- const email = profile.emails[0].value;
- let user = await User.findOne({
+ const googleClientSecret = await getClientSecretGoogle();
+ const googleClientId = await getClientIdGoogle();
+
+ passport.use(new GoogleStrategy({
+ passReqToCallback: true,
+ clientID: googleClientId,
+ clientSecret: googleClientSecret,
+ callbackURL: '/api/v1/oauth/callback/google',
+ scope: ['profile', ' email'],
+ }, async (
+ req: express.Request,
+ accessToken: string,
+ refreshToken: string,
+ profile: any,
+ cb: any
+ ) => {
+ try {
+ 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,
authId: profile.id,
- }).select('+publicKey')
-
- if (!user) {
- user = await new User({
- email,
- authProvider: AuthProvider.GOOGLE,
- authId: profile.id,
- }).save();
- }
-
- const providerAuthToken = createToken({
- payload: {
- userId: user._id.toString(),
- email: user.email,
- authProvider: user.authProvider,
- isUserCompleted: !!user.publicKey
- },
- expiresIn: await getJwtProviderAuthLifetime(),
- secret: await getJwtProviderAuthSecret(),
- });
-
- req.providerAuthToken = providerAuthToken;
- cb(null, profile);
- }));
+ }).save();
+ }
+
+ const providerAuthToken = createToken({
+ payload: {
+ userId: user._id.toString(),
+ email: user.email,
+ authProvider: user.authProvider,
+ isUserCompleted: !!user.publicKey
+ },
+ expiresIn: await getJwtProviderAuthLifetime(),
+ secret: await getJwtProviderAuthSecret(),
+ });
+
+ req.providerAuthToken = providerAuthToken;
+ cb(null, profile);
+ } catch (err) {
+ cb(err);
+ }
+ }));
}
export {
- getAuthDataPayloadIdObj,
- getAuthDataPayloadUserObj,
- initializePassport,
+ getAuthDataPayloadIdObj,
+ getAuthDataPayloadUserObj,
+ initializePassport,
}