Merge pull request #947 from automatisch/reset-password
feat: Add resetPassword mutation
This commit is contained in:
@@ -15,6 +15,7 @@ import deleteStep from './mutations/delete-step';
|
|||||||
import createUser from './mutations/create-user.ee';
|
import createUser from './mutations/create-user.ee';
|
||||||
import updateUser from './mutations/update-user';
|
import updateUser from './mutations/update-user';
|
||||||
import forgotPassword from './mutations/forgot-password.ee';
|
import forgotPassword from './mutations/forgot-password.ee';
|
||||||
|
import resetPassword from './mutations/reset-password.ee';
|
||||||
import login from './mutations/login';
|
import login from './mutations/login';
|
||||||
|
|
||||||
const mutationResolvers = {
|
const mutationResolvers = {
|
||||||
@@ -35,6 +36,7 @@ const mutationResolvers = {
|
|||||||
createUser,
|
createUser,
|
||||||
updateUser,
|
updateUser,
|
||||||
forgotPassword,
|
forgotPassword,
|
||||||
|
resetPassword,
|
||||||
login,
|
login,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
30
packages/backend/src/graphql/mutations/reset-password.ee.ts
Normal file
30
packages/backend/src/graphql/mutations/reset-password.ee.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import User from '../../models/user';
|
||||||
|
|
||||||
|
type Params = {
|
||||||
|
input: {
|
||||||
|
token: string;
|
||||||
|
password: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const resetPassword = async (_parent: unknown, params: Params) => {
|
||||||
|
const { token, password } = params.input;
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
throw new Error('Reset password token is required!');
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await User.query().findOne({ reset_password_token: token });
|
||||||
|
|
||||||
|
if (!user || !user.isResetPasswordTokenValid()) {
|
||||||
|
throw new Error(
|
||||||
|
'Reset password link is not valid or expired. Try generating a new link.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await user.resetPassword(password);
|
||||||
|
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default resetPassword;
|
@@ -51,6 +51,7 @@ type Mutation {
|
|||||||
createUser(input: CreateUserInput): User
|
createUser(input: CreateUserInput): User
|
||||||
updateUser(input: UpdateUserInput): User
|
updateUser(input: UpdateUserInput): User
|
||||||
forgotPassword(input: ForgotPasswordInput): Boolean
|
forgotPassword(input: ForgotPasswordInput): Boolean
|
||||||
|
resetPassword(input: ResetPasswordInput): Boolean
|
||||||
login(input: LoginInput): Auth
|
login(input: LoginInput): Auth
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -313,7 +314,12 @@ input UpdateUserInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
input ForgotPasswordInput {
|
input ForgotPasswordInput {
|
||||||
email: String
|
email: String!
|
||||||
|
}
|
||||||
|
|
||||||
|
input ResetPasswordInput {
|
||||||
|
token: String!
|
||||||
|
password: String!
|
||||||
}
|
}
|
||||||
|
|
||||||
input LoginInput {
|
input LoginInput {
|
||||||
|
@@ -87,6 +87,26 @@ class User extends Base {
|
|||||||
await this.$query().patch({ resetPasswordToken, resetPasswordTokenSentAt });
|
await this.$query().patch({ resetPasswordToken, resetPasswordTokenSentAt });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async resetPassword(password: string) {
|
||||||
|
return await this.$query().patch({
|
||||||
|
resetPasswordToken: null,
|
||||||
|
resetPasswordTokenSentAt: null,
|
||||||
|
password,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async isResetPasswordTokenValid() {
|
||||||
|
if (!this.resetPasswordTokenSentAt) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sentAt = new Date(this.resetPasswordTokenSentAt);
|
||||||
|
const now = new Date();
|
||||||
|
const fourHoursInMilliseconds = 1000 * 60 * 60 * 4;
|
||||||
|
|
||||||
|
return now.getTime() - sentAt.getTime() < fourHoursInMilliseconds;
|
||||||
|
}
|
||||||
|
|
||||||
async generateHash() {
|
async generateHash() {
|
||||||
this.password = await bcrypt.hash(this.password, 10);
|
this.password = await bcrypt.hash(this.password, 10);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user