Skip to content

Commit

Permalink
Final changes to auth login setup
Browse files Browse the repository at this point in the history
  • Loading branch information
AydanPirani committed Aug 14, 2023
1 parent 98a19ca commit 0cc30ba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
4 changes: 1 addition & 3 deletions api/services/auth/auth-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ export function generateJwtToken(payload: JwtPayload): string {
}

const options: SignOptions = {
// algorithm: "ES256",
expiresIn: "7d",
};

const token: string = jsonwebtoken.sign(payload, secret, options);
return token;
}


export function initializeRoles(provider: Provider): Role[] {
const roles: Role[] = [];

Expand All @@ -74,13 +74,11 @@ export function initializeRoles(provider: Provider): Role[] {

export async function getRoles(id: string, provider: Provider): Promise<Role[]> {
const collection: Collection = await DatabaseHelper.getCollection("auth", "roles");
console.log("in get roles! %s", id);
let roles: Role[] = [];

try {
const userRoles: RolesSchema | null = await collection.findOne({ id: id }) as RolesSchema | null;
if (userRoles == null) {
console.log("user not found! inserting");
roles = initializeRoles(provider);
const newUser: RolesSchema = { _id: new ObjectId(), id: id, provider: provider, roles: roles };
const insertResult: InsertOneResult = await collection.insertOne(newUser);
Expand Down
10 changes: 5 additions & 5 deletions api/services/auth/auth-router.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import "dotenv";
import express, { Request, Response, Router } from "express";
import passport from "passport";
import { NextFunction } from "express-serve-static-core";
import express, { Request, Response, Router } from "express";
import GitHubStrategy, { Profile as GithubProfile } from "passport-github";
import { Profile as GoogleProfile, Strategy as GoogleStrategy } from "passport-google-oauth20";

import Constants from "../../constants.js";
import { SelectAuthProvider } from "../../middleware/select-auth.js";
import { generateJwtToken as generateJwtToken, getJwtPayload, verifyFunction } from "./auth-lib.js";
import { JwtPayload, ProfileData, Provider } from "./auth-models.js";
import { NextFunction } from "express-serve-static-core";
import { generateJwtToken as generateJwtToken, getJwtPayload, verifyFunction } from "./auth-lib.js";


passport.use(Provider.GITHUB, new GitHubStrategy({
clientID: process.env.GITHUB_OAUTH_ID ?? "",
clientSecret: process.env.GITHUB_OAUTH_SECRET ?? "",
callbackURL: Constants.OAUTH_CALLBACK,
callbackURL: Constants.GITHUB_OAUTH_CALLBACK,
}, verifyFunction));


passport.use(Provider.GOOGLE, new GoogleStrategy({
clientID: process.env.GOOGLE_OAUTH_ID ?? "",
clientSecret: process.env.GOOGLE_OAUTH_SECRET ?? "",
callbackURL: Constants.OAUTH_CALLBACK,
callbackURL: Constants.GOOGLE_OAUTH_CALLBACK,
}, verifyFunction));


Expand Down

0 comments on commit 0cc30ba

Please sign in to comment.