Skip to content

Commit

Permalink
Merge pull request #72 from atlp-rwanda/187649830-bg-fix-default-role
Browse files Browse the repository at this point in the history
187649830 bg fix default role
  • Loading branch information
niyontwali authored May 29, 2024
2 parents fbbcea7 + 1feda53 commit 1d17fe7
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/config/passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import passport from 'passport';
import { Strategy as GoogleStrategy, StrategyOptionsWithRequest } from 'passport-google-oauth20';
import User, { UserAttributes } from '../database/models/user';
import getDefaultRole from '../helpers/defaultRoleGenerator';

// Defining options required for Google OAuth 2.0 authentication strategy
const googleStrategyOptions: StrategyOptionsWithRequest = {
Expand Down Expand Up @@ -30,7 +31,7 @@ passport.use(
return done(null, existingUser);
}

// If no existing user is found, create a new user
// If no existing user is found, create a new user, assigning a default role
const newUserAttributes: Partial<UserAttributes> = {
firstName: profile.name?.givenName,
lastName: profile.name?.familyName || '',
Expand All @@ -41,6 +42,7 @@ passport.use(
verified: true,
createdAt: new Date(),
updatedAt: new Date(),
RoleId: await getDefaultRole(),
};

const newUser = await User.create(newUserAttributes as UserAttributes);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const forgotPassword = async (req: Request, res: Response) => {
const token = await userToken(user.id, user.email);

// Send email with token
const link = `${process.env.URL_HOST}:${process.env.PORT}/api/auth/reset-password/${token}`;
const link = `${process.env.URL_HOST}/api/auth/reset-password/${token}`;

await sendEmail('reset_password', {
name: `${user.firstName} ${user.lastName}`,
Expand Down
1 change: 1 addition & 0 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export const editUserRole = async (req: Request, res: Response) => {

res.status(200).json({ ok: true, message: 'Role assigned successfully.' });
} catch (error) {
await transaction.rollback();
logger.error('Error Edit User Role: ', error);
sendInternalErrorResponse(res, error);
}
Expand Down
11 changes: 0 additions & 11 deletions src/database/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,6 @@ User.init(
sequelize: sequelize,
timestamps: true,
modelName: 'User',
hooks: {
beforeCreate: async (user: User) => {
try {
const defaultRole = await getDefaultRole();
user.RoleId = defaultRole;
} catch (error) {
logger.error('Error setting default role:', error);
throw error;
}
},
},
}
);

Expand Down

0 comments on commit 1d17fe7

Please sign in to comment.