fixing things across the board
This commit is contained in:
@@ -22,9 +22,11 @@ export const authCheck = async (
|
||||
"423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com", // Specify the CLIENT_ID of the app that accesses the backend
|
||||
});
|
||||
const userId = ticket.getPayload()?.sub;
|
||||
const email = ticket.getPayload()?.email;
|
||||
|
||||
// used in subsequent handlers
|
||||
res.locals.userId = userId;
|
||||
res.locals.email = email;
|
||||
|
||||
console.log(userId);
|
||||
|
||||
|
||||
@@ -24,22 +24,59 @@ export default function getUserRoutes() {
|
||||
* create user if doesn't exist
|
||||
*/
|
||||
async function getUserDetails(req: Request, res: Response) {
|
||||
const userId: string = res.locals.userId;
|
||||
const email: string = res.locals.email;
|
||||
|
||||
console.log(`getting data for ${userId}`);
|
||||
// passed in accesstoken no matter what
|
||||
const { access_token } = req.query;
|
||||
|
||||
console.log(`getting data for ${email}`);
|
||||
|
||||
try {
|
||||
// return user details if it passed auth middleware
|
||||
const user = await UserService.getUserById(userId);
|
||||
const user = await UserService.getUserByEmail(email);
|
||||
|
||||
// if no user found, then go ahead and create user
|
||||
if (!user) {
|
||||
if (!access_token) {
|
||||
res.status(400).send("No access token provided");
|
||||
return;
|
||||
}
|
||||
|
||||
// get google user info from access token
|
||||
const userInfo: GoogleUserInfo =
|
||||
await UserService.getGoogleUserInfoWithAccessToken(
|
||||
access_token as string
|
||||
);
|
||||
|
||||
// create initial user model object
|
||||
const newUser = new User(
|
||||
userInfo.email,
|
||||
userInfo.verifiedEmail,
|
||||
userInfo.name,
|
||||
userInfo.given_name,
|
||||
userInfo.family_name,
|
||||
userInfo.picture,
|
||||
userInfo.locale
|
||||
);
|
||||
|
||||
// create user if not exists
|
||||
const insertResult = await UserService.createUserIfNotExists(newUser);
|
||||
|
||||
insertResult
|
||||
? res.status(200).send("User created")
|
||||
: res.status(500).send("Failed to create account, already exists");
|
||||
}
|
||||
|
||||
// otherwise, just return the user details
|
||||
res.status(200).send(user);
|
||||
} catch (error) {
|
||||
res
|
||||
.status(404)
|
||||
.send(`unable to find a matching document with id: ${userId}`);
|
||||
.send(`unable to find a matching document with email: ${email}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** DEPRECATED...USING THE SAME SIGN IN ROUTE TO CREATE */
|
||||
async function createUser(req: Request, res: Response) {
|
||||
try {
|
||||
const { access_token } = req.query;
|
||||
|
||||
@@ -8,7 +8,27 @@ export class UserService {
|
||||
static async getUserById(userId: string) {
|
||||
const query = { _id: new ObjectId(userId) };
|
||||
|
||||
return (await collections.users?.findOne(query)) as unknown as User;
|
||||
const res = await collections.users?.findOne(query);
|
||||
|
||||
// exists
|
||||
if (res?._id) {
|
||||
return res as User;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static async getUserByEmail(email: string) {
|
||||
const query = { email };
|
||||
|
||||
const res = await collections.users?.findOne(query);
|
||||
|
||||
// exists
|
||||
if (res?._id) {
|
||||
return res as User;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static async createUserIfNotExists(newUser: User) {
|
||||
|
||||
Reference in New Issue
Block a user