decent place with fixing api and such
This commit is contained in:
@@ -44,8 +44,6 @@ async function createUser(req: Request, res: Response) {
|
|||||||
try {
|
try {
|
||||||
const { access_token } = req.query;
|
const { access_token } = req.query;
|
||||||
|
|
||||||
console.log(access_token);
|
|
||||||
|
|
||||||
if (!access_token) {
|
if (!access_token) {
|
||||||
res.status(400).send("No access token provided");
|
res.status(400).send("No access token provided");
|
||||||
return;
|
return;
|
||||||
@@ -57,11 +55,8 @@ async function createUser(req: Request, res: Response) {
|
|||||||
access_token as string
|
access_token as string
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(userInfo);
|
|
||||||
|
|
||||||
// create initial user model object
|
// create initial user model object
|
||||||
const newUser = new User(
|
const newUser = new User(
|
||||||
new ObjectId(userInfo.id),
|
|
||||||
userInfo.email,
|
userInfo.email,
|
||||||
userInfo.verifiedEmail,
|
userInfo.verifiedEmail,
|
||||||
userInfo.name,
|
userInfo.name,
|
||||||
@@ -71,14 +66,12 @@ async function createUser(req: Request, res: Response) {
|
|||||||
userInfo.locale
|
userInfo.locale
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(newUser);
|
|
||||||
|
|
||||||
// create user if not exists
|
// create user if not exists
|
||||||
const insertResult = await UserService.createUserIfNotExists(newUser);
|
const insertResult = await UserService.createUserIfNotExists(newUser);
|
||||||
|
|
||||||
insertResult
|
insertResult
|
||||||
? res.status(200).send("User created")
|
? 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) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
res.status(500).send("Problem in creating user");
|
res.status(500).send("Problem in creating user");
|
||||||
|
|||||||
@@ -12,14 +12,24 @@ export class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async createUserIfNotExists(newUser: User) {
|
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(
|
static async getGoogleUserInfoWithAccessToken(
|
||||||
accessToken: string
|
accessToken: string
|
||||||
): Promise<GoogleUserInfo> {
|
): Promise<GoogleUserInfo> {
|
||||||
return await axios.get(
|
return (
|
||||||
`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${accessToken}`
|
await axios.get(
|
||||||
);
|
`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${accessToken}`
|
||||||
|
)
|
||||||
|
).data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,14 @@ import { ObjectId } from "mongodb";
|
|||||||
|
|
||||||
export class User {
|
export class User {
|
||||||
constructor(
|
constructor(
|
||||||
public _id: ObjectId,
|
|
||||||
public email: string,
|
public email: string,
|
||||||
public verifiedEmail: boolean,
|
public verifiedEmail: boolean,
|
||||||
public name: string,
|
public name: string,
|
||||||
public given_name: string,
|
public given_name: string,
|
||||||
public family_name: string,
|
public family_name: string,
|
||||||
public picture: string,
|
public picture: string,
|
||||||
public locale: string
|
public locale: string,
|
||||||
|
public _id?: ObjectId
|
||||||
) // additional properties specific to our users collection
|
) // additional properties specific to our users collection
|
||||||
{}
|
{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ export async function handleLogin() {
|
|||||||
|
|
||||||
const tokens = await myApiOauth.openAuthWindowAndGetTokens();
|
const tokens = await myApiOauth.openAuthWindowAndGetTokens();
|
||||||
|
|
||||||
|
console.log(tokens);
|
||||||
|
|
||||||
store.set(STORE_ITEMS.AUTH_TOKENS, tokens);
|
store.set(STORE_ITEMS.AUTH_TOKENS, tokens);
|
||||||
|
|
||||||
browserWindow.webContents.send(Channels.AUTH_TOKENS, tokens);
|
browserWindow.webContents.send(Channels.AUTH_TOKENS, tokens);
|
||||||
|
|||||||
Reference in New Issue
Block a user