aboutsummaryrefslogtreecommitdiffstats
path: root/app/Controllers/Http/Dashboard/ForgotPasswordController.ts
blob: da05bbd51cafa229f96683c8f9e6fe285b130b1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
import { schema, rules, validator } from '@ioc:Adonis/Core/Validator';
import User from 'App/Models/User';

export default class ForgotPasswordController {
  /**
   * Display the forgot password form
   */
  public async show({ view }: HttpContextContract) {
    return view.render('dashboard/forgotPassword');
  }

  /**
   * Send forget password email to user
   */
  public async forgotPassword({ view, request }: HttpContextContract) {
    try {
      await validator.validate({
        schema: schema.create({
          mail: schema.string([rules.email(), rules.required()]),
        }),
        data: request.only(['mail']),
      });
    } catch {
      return view.render('others/message', {
        heading: 'Cannot reset your password',
        text: 'Please enter a valid email address',
      });
    }

    try {
      const user = await User.findByOrFail('email', request.input('mail'));
      await user.forgotPassword();
    } catch {}

    return view.render('others/message', {
      heading: 'Reset password',
      text: 'If your provided E-Mail address is linked to an account, we have just sent an E-Mail to that address.',
    });
  }
}