Password resets
@reset_token(email) starts a reset and returns a token for a link, or null when no user has that email. Tell the person the same thing either way, so the page doesn't show who has an account. The token works once, for http.auth.reset_lifetime (an hour), and a new one replaces the last. @reset_password(token, password) checks the new password, sets it, and ends the user's sessions. It returns the user, or null when the token is wrong, used or too old.
<import package="KScript.Http" />
<import package="KScript.Auth" />
<import package="KScript.Mail" />
<fn name="send_reset" params="form: map, request: map">
<let name="token" value="@reset_token($form.email)" />
<if $token != null>
<let name="link" value="'https://' + $request.headers.host + @route('password.reset', token: $token)" />
<do>@send_mail(to: $form.email, subject: 'Reset your password', view: 'mail/reset-password', data: { link: $link })</do>
</if>
<do>$session->flash('status', 'If that email has an account, a link is on its way.')</do>
<return value="@redirect(/forgot-password)" />
</fn>import KScript.Http
import KScript.Auth
import KScript.Mail
fn send_reset(form: map, request: map) {
$token = @reset_token($form.email)
if $token != null {
$link = 'https://' + $request.headers.host + @route('password.reset', token: $token)
@send_mail(to: $form.email, subject: 'Reset your password', view: 'mail/reset-password', data: { link: $link })
}
$session->flash('status', 'If that email has an account, a link is on its way.')
return @redirect(/forgot-password)
}The controller kscript make auth writes sends the link with @send_mail, using the views/mail/reset-password.html view it writes too. Until config.mail names a mail server, the message is written to the server's log, so you can follow the link while you build.