Skip to content

Commit

Permalink
SIKKA-6017[IN PROGRESS]
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansourkira committed Jan 6, 2024
1 parent 83255e2 commit 3f2c4e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sikka/hajar",
"version": "1.1.45",
"version": "1.1.46",
"description": "Toolkit to create SaaS applications",
"author": "Sikka Software <[email protected]> (http://sikka.io)",
"license": "MIT",
Expand Down
29 changes: 12 additions & 17 deletions src/core/authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class HajarAuth {
}
}

async loginCustomerGoogle(googleUserData, isGoogle = true) {
async loginCustomerGoogle(googleUserData) {
try {
let user;

Expand All @@ -237,30 +237,25 @@ class HajarAuth {
});

if (!user) {
// User doesn't exist, perform registration
user = new this.User({
username: googleUserData.username,
email: googleUserData.email,
ref: "customers",
password: await this.bcrypt.hash(googleUserData.password, 10),
// role: googleUserData.role,
});

await user.save();

let customerData = await this.Customer.findOne({ uid: user._id });
let customerData = new this.Customer({
profile: user._id,
uid: user._id,
username: googleUserData.username,
firstName: googleUserData.firstName,
lastName: googleUserData.lastName,
});

if (!customerData) {
customerData = new this.Customer({
profile: user._id,
// role: googleUserData.role,
uid: user._id,
username: googleUserData.username,
firstName: googleUserData.firstName,
lastName: googleUserData.lastName,
});

await customerData.save();
}
await customerData.save();

return {
success: true,
Expand All @@ -270,6 +265,7 @@ class HajarAuth {
token: this.jwt.sign({ userId: user._id }, this.secret),
};
} else {
// User exists, perform login
const isPasswordCorrect = await this.bcrypt.compare(
googleUserData.password,
user.password
Expand All @@ -283,13 +279,12 @@ class HajarAuth {
}

const customerData = await this.Customer.findOne({ profile: user._id });
const token = this.jwt.sign({ userId: user._id }, this.secret);
return {
success: true,
user: { ...user.toObject() },
message: "Login successful",
customer: { ...customerData.toObject() },
token,
token: this.jwt.sign({ userId: user._id }, this.secret),
};
}
} catch (error) {
Expand Down

0 comments on commit 3f2c4e5

Please sign in to comment.