Skip to content

Commit

Permalink
SIKKA-6202[IN PROGRESS]
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansourkira committed Jan 6, 2024
1 parent 0db4f3a commit 83255e2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 44 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.44",
"version": "1.1.45",
"description": "Toolkit to create SaaS applications",
"author": "Sikka Software <[email protected]> (http://sikka.io)",
"license": "MIT",
Expand Down
74 changes: 31 additions & 43 deletions src/core/authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,23 +230,22 @@ class HajarAuth {
async loginCustomerGoogle(googleUserData, isGoogle = true) {
try {
let user;
if (isGoogle) {
user = await this.User.findOne({

user = await this.User.findOne({
email: googleUserData.email,
ref: "customers",
});

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

if (!user) {
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();
}
await user.save();

let customerData = await this.Customer.findOne({ uid: user._id });

Expand All @@ -270,40 +269,29 @@ class HajarAuth {
message: "Registration success",
token: this.jwt.sign({ userId: user._id }, this.secret),
};
}
user = await this.User.findOne({
email: googleUserData.email,
ref: "customers",
});

if (!user) {
throw new HajarError(
"Invalid email or password",
"invalid-email-password"
} else {
const isPasswordCorrect = await this.bcrypt.compare(
googleUserData.password,
user.password
);
}

const isPasswordCorrect = await this.bcrypt.compare(
googleUserData.password,
user.password
);
if (!isPasswordCorrect) {
throw new HajarError(
"Invalid email or password",
"invalid-email-password"
);
}

if (!isPasswordCorrect) {
throw new HajarError(
"Invalid email or password",
"invalid-email-password"
);
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,
};
}

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,
};
} catch (error) {
console.error(error);
return new HajarError(error.message, "customer-login-error");
Expand Down

0 comments on commit 83255e2

Please sign in to comment.