-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use hookable for login and logout functions
- Loading branch information
Showing
8 changed files
with
98 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/evershop/src/modules/auth/services/loginUserWithEmail.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const { pool } = require('@evershop/evershop/src/lib/postgres/connection'); | ||
const { | ||
comparePassword | ||
} = require('@evershop/evershop/src/lib/util/passwordHelper'); | ||
const { select } = require('@evershop/postgres-query-builder'); | ||
|
||
/** | ||
* This function will login the admin user with email and password. This function must be accessed from the request object (request.loginUserWithEmail(email, password, callback)) | ||
* @param {string} email | ||
* @param {string} password | ||
*/ | ||
async function loginUserWithEmail(email, password) { | ||
// Escape the email to prevent SQL injection | ||
const userEmail = email.replace(/%/g, '\\%'); | ||
const user = await select() | ||
.from('admin_user') | ||
.where('email', 'ILIKE', userEmail) | ||
.and('status', '=', 1) | ||
.load(pool); | ||
const result = comparePassword(password, user ? user.password : ''); | ||
if (!user || !result) { | ||
throw new Error('Invalid email or password'); | ||
} | ||
this.session.userID = user.admin_user_id; | ||
// Delete the password field | ||
delete user.password; | ||
// Save the user in the request | ||
this.locals.user = user; | ||
} | ||
|
||
module.exports = loginUserWithEmail; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Logout a current user. This function must be accessed from the request object (request.logoutUser(callback)) | ||
*/ | ||
function logoutUser() { | ||
this.session.userID = undefined; | ||
this.locals.user = undefined; | ||
} | ||
|
||
module.exports = logoutUser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/evershop/src/modules/customer/services/customer/loginCustomerWithEmail.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const { | ||
translate | ||
} = require('@evershop/evershop/src/lib/locale/translate/translate'); | ||
const { pool } = require('@evershop/evershop/src/lib/postgres/connection'); | ||
const { | ||
comparePassword | ||
} = require('@evershop/evershop/src/lib/util/passwordHelper'); | ||
const { select } = require('@evershop/postgres-query-builder'); | ||
/** | ||
* Login a customer with email and password. This function must be accessed from the request object (request.loginCustomerWithEmail(email, password, callback)) | ||
* @param {string} email | ||
* @param {string} password | ||
*/ | ||
async function loginCustomerWithEmail(email, password) { | ||
// Escape the email to prevent SQL injection | ||
const customerEmail = email.replace(/%/g, '\\%'); | ||
const customer = await select() | ||
.from('customer') | ||
.where('email', 'ILIKE', customerEmail) | ||
.and('status', '=', 1) | ||
.load(pool); | ||
const result = comparePassword(password, customer ? customer.password : ''); | ||
if (!customer || !result) { | ||
throw new Error(translate('Invalid email or password')); | ||
} | ||
this.session.customerID = customer.customer_id; | ||
// Delete the password field | ||
delete customer.password; | ||
// Save the customer in the request | ||
this.locals.customer = customer; | ||
} | ||
|
||
module.exports = loginCustomerWithEmail; |
9 changes: 9 additions & 0 deletions
9
packages/evershop/src/modules/customer/services/customer/logoutCustomer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Logout the current customer. This function must be accessed from the request object (request.logoutCustomer(callback)) | ||
*/ | ||
function logoutCustomer() { | ||
this.session.customerID = undefined; | ||
this.locals.customer = undefined; | ||
} | ||
|
||
module.exports = logoutCustomer; |