Skip to content

Commit

Permalink
chore : updating customer to clients in hajarauth customer functions
Browse files Browse the repository at this point in the history
SIKKA-6447[in progress]
  • Loading branch information
Mansourkira committed Mar 4, 2024
1 parent c9e881d commit 45c5d0f
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions src/core/authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class HajarAuth {
}
}

async registerCustomer(userDetails) {
async registerClient(userDetails) {
try {
userDetails.email = userDetails.email.toLowerCase();
const userExists = await this.User.findOne({ email: userDetails.email });
Expand Down Expand Up @@ -127,13 +127,13 @@ class HajarAuth {
const user = new this.User({
username: userDetails.username,
email: userDetails.email,
ref: "customers",
ref: "clients",
password: hashedPassword,
});

const newUser = await user.save();

const customer = new this.Customer({
const client = new this.Client({
profile: newUser._id,
uid: newUser._id,
username: userDetails.username,
Expand All @@ -147,22 +147,21 @@ class HajarAuth {
},
});

const newCustomer = await customer.save();
const newClient = await client.save();

const token = this.jwt.sign({ userId: newUser._id }, this.secret);

return {
success: true,
user: { ...newUser.toObject() },
customer: { ...newCustomer.toObject() },
client: { ...newClient.toObject() },
token,
};
} catch (error) {
console.error("Registration error:", error);
throw error;
}
}

async createRole({ roleName, permissionIds, ...newRoleOptions }) {
const existingRole = await this.Role.findOne({ name: roleName });
if (existingRole) {
Expand Down Expand Up @@ -235,67 +234,62 @@ class HajarAuth {
}
}

async loginCustomerGoogle(googleUserData) {
async loginClientGoogle(googleUserData) {
try {
let user;

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

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

await user.save();

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

await customerData.save();
await clientData.save();

return {
success: true,
user: user,
customer: customerData,
client: clientData,
message: "Registration success",
token: this.jwt.sign({ userId: user._id }, this.secret),
};
} else {
// User exists, perform login
const customerData = await this.Customer.findOne({ profile: user._id });
const clientData = await this.Client.findOne({ profile: user._id });
return {
success: true,
user: { ...user.toObject() },
message: "Login successful",
customer: { ...customerData.toObject() },
client: { ...clientData.toObject() },
token: this.jwt.sign({ userId: user._id }, this.secret),
refreshToken: this.jwt.sign(
{ userId: user._id },
this.refreshSecret,
{ expiresIn: "7d" }
), // Add refresh token
};
}
} catch (error) {
console.error(error);
return new HajarError(error.message, "customer-login-error");
return new HajarError(error.message, "client-login-error");
}
}
async loginCustomer(email, password, isGoogle = false) {
async loginClient(email, password, isGoogle = false) {
try {
const user = await this.User.findOne({ email: email, ref: "customers" });
const user = await this.User.findOne({ email: email, ref: "clients" });

if (!user) {
throw new HajarError(
Expand All @@ -317,21 +311,18 @@ class HajarAuth {
);
}
}
const customerData = await this.Customer.findOne({ profile: user._id });
const clientData = await this.Client.findOne({ profile: user._id });

const token = this.jwt.sign({ userId: user._id }, this.secret);
return {
success: true,
user: { ...user.toObject() },
customer: { ...customerData.toObject() },
client: { ...clientData.toObject() },
token,
refreshToken: this.jwt.sign({ userId: user._id }, this.refreshSecret, {
expiresIn: "7d",
}), // Add refresh token
};
} catch (error) {
console.error(error);
return new HajarError(error.message, "customer-login-error");
return new HajarError(error.message, "client-login-error");
}
}

Expand Down

0 comments on commit 45c5d0f

Please sign in to comment.