aboutsummaryrefslogtreecommitdiffstats
path: root/app/Controllers/Http/Dashboard/ForgotPasswordController.ts
blob: f7b1d0ebca776f6cf399b4bd77d8b2b8894f382c (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 { HttpContext } from '@adonisjs/core/http'
import { schema, rules, validator } from '@adonisjs/validator'
import User from '#app/Models/User'

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

  /**
   * Send forget password email to user
   */
  public async forgotPassword({ view, request }: HttpContext) {
    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.',
    })
  }
}