decent place with fixing api and such

This commit is contained in:
talksik
2022-03-17 23:12:19 -04:00
parent 013d2fcb16
commit 4d0a75af6e
4 changed files with 19 additions and 14 deletions
+1 -8
View File
@@ -44,8 +44,6 @@ async function createUser(req: Request, res: Response) {
try {
const { access_token } = req.query;
console.log(access_token);
if (!access_token) {
res.status(400).send("No access token provided");
return;
@@ -57,11 +55,8 @@ async function createUser(req: Request, res: Response) {
access_token as string
);
console.log(userInfo);
// create initial user model object
const newUser = new User(
new ObjectId(userInfo.id),
userInfo.email,
userInfo.verifiedEmail,
userInfo.name,
@@ -71,14 +66,12 @@ async function createUser(req: Request, res: Response) {
userInfo.locale
);
console.log(newUser);
// 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 new user");
: res.status(500).send("Failed to create account, already exists");
} catch (error) {
console.log(error);
res.status(500).send("Problem in creating user");
+14 -4
View File
@@ -12,14 +12,24 @@ export class UserService {
}
static async createUserIfNotExists(newUser: User) {
return await collections.users?.insertOne(newUser);
const exists = (await collections.users?.findOne({ email: newUser.email }))
?._id;
if (!exists) {
return await collections.users?.insertOne(newUser);
}
// user with email exists already, don't create
return null;
}
static async getGoogleUserInfoWithAccessToken(
accessToken: string
): Promise<GoogleUserInfo> {
return await axios.get(
`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${accessToken}`
);
return (
await axios.get(
`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${accessToken}`
)
).data;
}
}
+2 -2
View File
@@ -2,14 +2,14 @@ import { ObjectId } from "mongodb";
export class User {
constructor(
public _id: ObjectId,
public email: string,
public verifiedEmail: boolean,
public name: string,
public given_name: string,
public family_name: string,
public picture: string,
public locale: string
public locale: string,
public _id?: ObjectId
) // additional properties specific to our users collection
{}
}
@@ -37,6 +37,8 @@ export async function handleLogin() {
const tokens = await myApiOauth.openAuthWindowAndGetTokens();
console.log(tokens);
store.set(STORE_ITEMS.AUTH_TOKENS, tokens);
browserWindow.webContents.send(Channels.AUTH_TOKENS, tokens);