Merge pull request #1342 from automatisch/lowercase-email
fix: Lowercase user email before insert and update
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
import { Knex } from 'knex';
|
||||||
|
|
||||||
|
export async function up(knex: Knex): Promise<void> {
|
||||||
|
await knex('users')
|
||||||
|
.whereRaw('email != LOWER(email)')
|
||||||
|
.update({
|
||||||
|
email: knex.raw('LOWER(email)'),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(): Promise<void> {
|
||||||
|
// void
|
||||||
|
}
|
@@ -13,12 +13,18 @@ type Params = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const createUser = async (_parent: unknown, params: Params, context: Context) => {
|
const createUser = async (
|
||||||
|
_parent: unknown,
|
||||||
|
params: Params,
|
||||||
|
context: Context
|
||||||
|
) => {
|
||||||
context.currentUser.can('create', 'User');
|
context.currentUser.can('create', 'User');
|
||||||
|
|
||||||
const { fullName, email, password } = params.input;
|
const { fullName, email, password } = params.input;
|
||||||
|
|
||||||
const existingUser = await User.query().findOne({ email });
|
const existingUser = await User.query().findOne({
|
||||||
|
email: email.toLowerCase(),
|
||||||
|
});
|
||||||
|
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
throw new Error('User already exists!');
|
throw new Error('User already exists!');
|
||||||
|
@@ -15,7 +15,7 @@ type Params = {
|
|||||||
const forgotPassword = async (_parent: unknown, params: Params) => {
|
const forgotPassword = async (_parent: unknown, params: Params) => {
|
||||||
const { email } = params.input;
|
const { email } = params.input;
|
||||||
|
|
||||||
const user = await User.query().findOne({ email });
|
const user = await User.query().findOne({ email: email.toLowerCase() });
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new Error('Email address not found!');
|
throw new Error('Email address not found!');
|
||||||
|
@@ -12,7 +12,9 @@ type Params = {
|
|||||||
const registerUser = async (_parent: unknown, params: Params) => {
|
const registerUser = async (_parent: unknown, params: Params) => {
|
||||||
const { fullName, email, password } = params.input;
|
const { fullName, email, password } = params.input;
|
||||||
|
|
||||||
const existingUser = await User.query().findOne({ email });
|
const existingUser = await User.query().findOne({
|
||||||
|
email: email.toLowerCase(),
|
||||||
|
});
|
||||||
|
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
throw new Error('User already exists!');
|
throw new Error('User already exists!');
|
||||||
|
@@ -263,6 +263,8 @@ class User extends Base {
|
|||||||
|
|
||||||
async $beforeInsert(queryContext: QueryContext) {
|
async $beforeInsert(queryContext: QueryContext) {
|
||||||
await super.$beforeInsert(queryContext);
|
await super.$beforeInsert(queryContext);
|
||||||
|
|
||||||
|
this.email = this.email.toLowerCase();
|
||||||
await this.generateHash();
|
await this.generateHash();
|
||||||
|
|
||||||
if (appConfig.isCloud) {
|
if (appConfig.isCloud) {
|
||||||
@@ -273,6 +275,7 @@ class User extends Base {
|
|||||||
async $beforeUpdate(opt: ModelOptions, queryContext: QueryContext) {
|
async $beforeUpdate(opt: ModelOptions, queryContext: QueryContext) {
|
||||||
await super.$beforeUpdate(opt, queryContext);
|
await super.$beforeUpdate(opt, queryContext);
|
||||||
|
|
||||||
|
this.email = this.email.toLowerCase();
|
||||||
await this.generateHash();
|
await this.generateHash();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user