Skip to content

Commit

Permalink
Added changes for database insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
AydanPirani committed Apr 11, 2024
1 parent 149d707 commit 87e2d3a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mongoose, { Schema } from "mongoose";
import { RoleInfo, RoleSchema } from "./services/auth/auth-schema";
import { RoleValidator, RoleSchema } from "./services/auth/auth-schema";

mongoose.set("toObject", { versionKey: false });

Expand Down Expand Up @@ -31,5 +31,5 @@ function initializeModel(

// Example usage
export const Database = {
ROLES: initializeModel("roles", RoleSchema, RoleInfo),
ROLES: initializeModel("roles", RoleSchema, RoleValidator),
};
2 changes: 1 addition & 1 deletion src/services/auth/auth-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ authRouter.get(
session: false,
})(req, res, next),
function (req, res) {
const redirectUri = `${DeviceRedirects[req.params.DEVICE]}`
const redirectUri = `${DeviceRedirects[req.params.DEVICE]}`;
return res.redirect(redirectUri);
}
);
Expand Down
42 changes: 25 additions & 17 deletions src/services/auth/auth-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@ export const Role = z.enum(["USER", "ADMIN", "CORPORATE"]);

export const RoleValidator = z.object({
userId: z.coerce.string().regex(/user[0-9]*/),
name: z.coerce.string(),
email: z.coerce.string().email(),
roles: z.array(Role),
});

export const RoleSchema = new Schema({
userId: {
type: String,
required: true,
unique: true,
export const RoleSchema = new Schema(
{
userId: {
type: String,
required: true,
unique: true,
},
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
roles: {
type: [String],
enum: Role.Values,
default: [],
required: true,
},
},
email: {
type: String,
required: true,
unique: true,
},
roles: {
type: [String],
enum: Role.Values,
default: [],
required: true,
},
});
{ timestamps: { createdAt: "createdAt" } }
);
19 changes: 16 additions & 3 deletions src/services/auth/auth-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Create a function to generate GoogleStrategy instances
import { Strategy as GoogleStrategy } from "passport-google-oauth20";
import { Config } from "../../config";
import { Role } from "./auth-schema";
import { Database } from "../../database";

export function createGoogleStrategy(device: string) {
return new GoogleStrategy(
Expand All @@ -9,10 +11,21 @@ export function createGoogleStrategy(device: string) {
clientSecret: Config.CLIENT_SECRET,
callbackURL: `${Config.AUTH_CALLBACK_URI_BASE}${device}`,
},

// Strategy -> insert user into database if they don't exist
async function (_1, _2, profile, cb) {
// Add profile to database here
console.log(profile);
cb(null, profile);
const userId = `user${profile.id}`;
const name = profile.displayName;
const email = profile._json.email;
const roles = [Role.Enum.USER];

Database.ROLES.findOneAndUpdate(
{ userId: userId },
{ userId, name, email, roles },
{ upsert: true }
)
.then(() => cb(null, profile))
.catch((err) => cb(err, profile));
}
);
}

0 comments on commit 87e2d3a

Please sign in to comment.