Skip to content

Commit

Permalink
Notifications
Browse files Browse the repository at this point in the history
- Added registration and forgot password notifications
  • Loading branch information
deanblackborough committed Jan 27, 2021
1 parent 761d4e6 commit 18217ff
Show file tree
Hide file tree
Showing 21 changed files with 614 additions and 18 deletions.
20 changes: 8 additions & 12 deletions app/Http/Controllers/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\PasswordCreates;
use App\Models\PasswordResets;
use App\Notifications\ForgotPassword;
use App\Notifications\Registered;
use App\User;
use Illuminate\Http;
Expand All @@ -22,7 +23,7 @@ public function check(): Http\JsonResponse

public function createPassword(): Http\JsonResponse
{
$email = Str::replaceFirst(' ', '+', request()->query('email'));
$email = Str::replaceFirst(' ', '+', urldecode(request()->query('email')));
$token = request()->query('token');

$tokens = DB::table('password_creates')
Expand All @@ -32,7 +33,7 @@ public function createPassword(): Http\JsonResponse
if ($tokens === null || Hash::check($token, $tokens->token) === false) {
return response()->json(
[
'message'=>'Sorry, the email and token you supplied are invalid'
'message'=>'Sorry, the email and or token you supplied are invalid'
],
401
);
Expand Down Expand Up @@ -86,7 +87,7 @@ public function createPassword(): Http\JsonResponse

public function createNewPassword(): Http\JsonResponse
{
$email = Str::replaceFirst(' ', '+', request()->query('email'));
$email = Str::replaceFirst(' ', '+', urldecode(request()->query('email')));
$token = request()->query('token');

$tokens = DB::table('password_resets')
Expand Down Expand Up @@ -175,25 +176,22 @@ public function forgotPassword(): Http\JsonResponse

if ($user !== null) {
try {
$user->password = Hash::make(Str::random(20));
$user->save();

$create_token = Str::random(20);

$password = new PasswordResets();
$password->email = $email;
$password->token = Hash::make($create_token);
$password->created_at = now()->toDateTimeString();
$password->save();

$user->notify(new ForgotPassword($user, $create_token));
} catch (\Exception $e) {
return response()->json(['error' => 'Unable to process your forgot password request, please try again later'], 500);
}

return response()->json(
[
'message' => "You can generate a new password by POSTing 'password' and 'confirm_password' to " .
'v2/auth/create-new-password?token=' . $create_token .
'&email=' . $email
'message' => 'Request received, please check your email for instructions on how to create your new password'
],
201
);
Expand Down Expand Up @@ -277,9 +275,7 @@ public function register(): Http\JsonResponse

return response()->json(
[
'message' => "Your account has been created, please POST 'password' and 'confirm_password' to " .
'v2/auth/create-password?token=' . $create_token .
'&email=' . $email . ' to create your password'
'message' => 'Account created, please check you email, we include instructions on creating your password'
],
201
);
Expand Down
57 changes: 57 additions & 0 deletions app/Notifications/ForgotPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Notifications;

use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class ForgotPassword extends Notification implements ShouldQueue
{
use Queueable;

private User $user;
private string $token;

public function __construct(User $user, string $token)
{
$this->user = $user;
$this->token = $token;
}

public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Costs to Expect API: Reset password')
->greeting('Hi,')
->line('We have received a request to reset your password, if this we you please follow the steps below, if this was not you, please ignore this email and let us know.')
->line("To create a new password please POST `password` and `password_confirmation` to " . url('/v2/auth/create-new-password?email=' . urlencode($this->user->email) . '&token=' . urlencode($this->token)))
->line('Thank you for using the Costs to Expect API.');
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
14 changes: 8 additions & 6 deletions app/Notifications/Registered.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class Registered extends Notification
class Registered extends Notification implements ShouldQueue
{
use Queueable;

Expand All @@ -30,12 +30,14 @@ public function toMail($notifiable)
{
return
(new MailMessage)
->subject('Costs to Expect API: Account registration')
->greeting('Hi,')
->line('Thank you for registering with the Costs to Expect API.')
->line('To use the Costs to Expect API you need to complete your account. To complete your account you need to create a password')
->line("To create a password please POST `password` and `password_confirmation` to " . url('/v2/create-password?email=' . urlencode($this->user->email) . '&token=' . urlencode($this->token)))
->line('The full documentation for the API is available online')
->action('Read documentation', url('https://postman.costs-to-expect.com/'))
->line('Thank you for using the Costs to Expect API');
->line('To use the Costs to Expect API you need to complete your account. To complete your account you need to create a password.')
->line("To create a password please POST `password` and `password_confirmation` to " . url('/v2/auth/create-password?email=' . urlencode($this->user->email) . '&token=' . urlencode($this->token)))
->line('Whilst we have you, the documentation for the API is available online.')
->action('Read our documentation', url('https://postman.costs-to-expect.com/'))
->line('Thank you for using the Costs to Expect API.');
}

public function toArray($notifiable)
Expand Down
19 changes: 19 additions & 0 deletions resources/views/vendor/mail/html/button.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<table class="action" align="center" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td align="center">
<table width="100%" border="0" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td align="center">
<table border="0" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td>
<a href="{{ $url }}" class="button button-{{ $color ?? 'primary' }}" target="_blank" rel="noopener">{{ $slot }}</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
11 changes: 11 additions & 0 deletions resources/views/vendor/mail/html/footer.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<tr>
<td>
<table class="footer" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="content-cell" align="center">
{{ Illuminate\Mail\Markdown::parse($slot) }}
</td>
</tr>
</table>
</td>
</tr>
11 changes: 11 additions & 0 deletions resources/views/vendor/mail/html/header.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<tr>
<td class="header">
<a href="{{ $url }}" style="display: inline-block;">
@if (trim($slot) === 'Laravel')
<img src="https://laravel.com/img/notification-logo.png" class="logo" alt="Laravel Logo">
@else
{{ $slot }}
@endif
</a>
</td>
</tr>
56 changes: 56 additions & 0 deletions resources/views/vendor/mail/html/layout.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="color-scheme" content="light">
<meta name="supported-color-schemes" content="light">
</head>
<body>
<style>
@media only screen and (max-width: 600px) {
.inner-body {
width: 100% !important;
}
.footer {
width: 100% !important;
}
}
@media only screen and (max-width: 500px) {
.button {
width: 100% !important;
}
}
</style>

<table class="wrapper" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td align="center">
<table class="content" width="100%" cellpadding="0" cellspacing="0" role="presentation">
{{ $header ?? '' }}

<!-- Email Body -->
<tr>
<td class="body" width="100%" cellpadding="0" cellspacing="0">
<table class="inner-body" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
<!-- Body content -->
<tr>
<td class="content-cell">
{{ Illuminate\Mail\Markdown::parse($slot) }}

{{ $subcopy ?? '' }}
</td>
</tr>
</table>
</td>
</tr>

{{ $footer ?? '' }}
</table>
</td>
</tr>
</table>
</body>
</html>
27 changes: 27 additions & 0 deletions resources/views/vendor/mail/html/message.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url')])
Costs to Expect
@endcomponent
@endslot

{{-- Body --}}
{{ $slot }}

{{-- Subcopy --}}
@isset($subcopy)
@slot('subcopy')
@component('mail::subcopy')
{{ $subcopy }}
@endcomponent
@endslot
@endisset

{{-- Footer --}}
@slot('footer')
@component('mail::footer')
© {{ date('Y') }} Costs to Expect and Dean Blackborough. @lang('All rights reserved.')
@endcomponent
@endslot
@endcomponent
14 changes: 14 additions & 0 deletions resources/views/vendor/mail/html/panel.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<table class="panel" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="panel-content">
<table width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="panel-item">
{{ Illuminate\Mail\Markdown::parse($slot) }}
</td>
</tr>
</table>
</td>
</tr>
</table>

7 changes: 7 additions & 0 deletions resources/views/vendor/mail/html/subcopy.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<table class="subcopy" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td>
{{ Illuminate\Mail\Markdown::parse($slot) }}
</td>
</tr>
</table>
3 changes: 3 additions & 0 deletions resources/views/vendor/mail/html/table.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="table">
{{ Illuminate\Mail\Markdown::parse($slot) }}
</div>
Loading

0 comments on commit 18217ff

Please sign in to comment.