Skip to content

Commit

Permalink
Add password reset system, add lang to response messages
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredobarron committed May 7, 2018
1 parent b149321 commit edf7b30
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 17 deletions.
15 changes: 12 additions & 3 deletions README.md
@@ -1,15 +1,17 @@
# Modulr API Laravel Passport

API documentation [here](https://documenter.getpostman.com/view/1657780/RW1ejGzL) in postman

## Table of Contents

- [Install](#install)
- [Installation](#installation)
- [Dependencies](#dependencies)
- [Routes](#routes)
- [Authentication](#authentication)
- [Password Reset](#password-reset)


### Install
### Installation

1. Clone repository
```
Expand Down Expand Up @@ -55,7 +57,7 @@ DB_PASSWORD=secret
### Dependencies


- [laravolt/avatar](https://github.com/laravolt/avatar) - Generate Avatars
- [laravolt/avatar](https://github.com/laravolt/avatar) - Generate avatars for users of application


### Routes
Expand All @@ -67,3 +69,10 @@ DB_PASSWORD=secret
- POST /auth/signup
- GET /auth/signup/activate/{token}
- GET /auth/user


##### Password Reset

- POST /password/create
- GET /password/find/{token}
- POST /password/reset
25 changes: 13 additions & 12 deletions app/Http/Controllers/Auth/AuthController.php
Expand Up @@ -3,8 +3,8 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Storage;
use Avatar;
Expand All @@ -21,7 +21,7 @@ class AuthController extends Controller
* @param [string] email
* @param [string] password
* @param [string] password_confirmation
* @return [json] message, errors
* @return [string] message
*/
public function signup(Request $request)
{
Expand All @@ -46,15 +46,15 @@ public function signup(Request $request)
$user->notify(new SignupActivate($user));

return response()->json([
'message' => 'Successfully created user!'
'message' => __('auth.signup_success')
], 201);
}

/**
* Confirm your account activate user
*
* @param [type] $token
* @return [string] error
* @return [string] message
* @return [obj] user
*/
public function signupActivate($token)
Expand All @@ -63,7 +63,7 @@ public function signupActivate($token)

if (!$user) {
return response()->json([
'message' => 'This activation token is invalid.'
'message' => __('auth.token_invalid')
], 404);
}

Expand All @@ -82,7 +82,9 @@ public function signupActivate($token)
* @param [string] email
* @param [string] password
* @param [boolean] remember_me
* @return [json] token, error
* @return [string] access_token
* @return [string] token_type
* @return [string] expires_at
*/
public function login(Request $request)
{
Expand All @@ -95,11 +97,10 @@ public function login(Request $request)
$credentials['active'] = 1;
$credentials['deleted_at'] = null;

if(!Auth::attempt($credentials)){
if(!Auth::attempt($credentials))
return response()->json([
'message' => 'Unauthorized'
'message' => __('auth.login_failed')
], 401);
}

$user = $request->user();

Expand All @@ -121,21 +122,21 @@ public function login(Request $request)
/**
* Log the user out (Invalidate the token).
*
* @return [json] message
* @return [string] message
*/
public function logout(Request $request)
{
$request->user()->token()->revoke();

return response()->json([
'message' => 'Successfully logged out'
'message' => __('auth.logout_success')
]);
}

/**
* Get the authenticated User.
*
* @return [json] user obj
* @return [json] user object
*/
public function user(Request $request)
{
Expand Down
114 changes: 114 additions & 0 deletions app/Http/Controllers/Auth/PasswordResetController.php
@@ -0,0 +1,114 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Notifications\PasswordResetRequest;
use App\Notifications\PasswordResetSuccess;
use App\User;
use App\PasswordReset;

class PasswordResetController extends Controller
{
/**
* Create token password reset
*
* @param [string] email
* @return [string] message
*/
public function create(Request $request)
{
$request->validate([
'email' => 'required|string|email',
]);

$user = User::where('email', $request->email)->first();

if (!$user)
return response()->json([
'message' => __('passwords.user')
], 404);

$passwordReset = PasswordReset::updateOrCreate(['email' => $user->email], [
'email' => $user->email,
'token' => str_random(60)
]);

if ($user && $passwordReset)
$user->notify(new PasswordResetRequest($passwordReset->token));

return response()->json([
'message' => __('passwords.sent')
]);
}

/**
* Find token password reset
*
* @param [string] $token
* @return [string] message
* @return [json] passwordReset object
*/
public function find($token)
{
$passwordReset = PasswordReset::where('token', $token)->first();

if (!$passwordReset)
return response()->json([
'message' => __('passwords.token')
], 404);

if (Carbon::parse($passwordReset->updated_at)->addMinutes(720)->isPast()) {
$passwordReset->delete();
return response()->json([
'message' => __('passwords.token')
], 404);
}

return response()->json($passwordReset);
}

/**
* Reset password
*
* @param [string] email
* @param [string] password
* @param [string] password_confirmation
* @param [string] token
* @return [string] message
* @return [json] user object
*/
public function reset(Request $request)
{
$request->validate([
'email' => 'required|string|email',
'password' => 'required|string|confirmed',
'token' => 'required|string'
]);

$passwordReset = PasswordReset::where('token', $request->token)->first();

if (!$passwordReset)
return response()->json([
'message' => __('passwords.token')
], 404);

$user = User::where('email', $request->email)->first();

if (!$user)
return response()->json([
'message' => __('passwords.user')
], 404);

$user->password = bcrypt($request->password);
$user->save();

$passwordReset->delete();

$user->notify(new PasswordResetSuccess($passwordReset));

return response()->json($user);
}
}
65 changes: 65 additions & 0 deletions app/Notifications/PasswordResetRequest.php
@@ -0,0 +1,65 @@
<?php

namespace App\Notifications;

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

class PasswordResetRequest extends Notification implements ShouldQueue
{
use Queueable;

protected $token;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($token)
{
$this->token = $token;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
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)
{
$url = url('/api/password/find/'.$this->token);

return (new MailMessage)
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', url($url))
->line('If you did not request a password reset, no further action is required.');
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
61 changes: 61 additions & 0 deletions app/Notifications/PasswordResetSuccess.php
@@ -0,0 +1,61 @@
<?php

namespace App\Notifications;

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

class PasswordResetSuccess extends Notification implements ShouldQueue
{
use Queueable;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
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)
->line('You are receiving this email because changed your password for your account.')
->line('If you did change password, no further action is required.')
->line('If you did not change password, protect your account.');
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
12 changes: 12 additions & 0 deletions app/PasswordReset.php
@@ -0,0 +1,12 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PasswordReset extends Model
{
protected $fillable = [
'email', 'token'
];
}

0 comments on commit edf7b30

Please sign in to comment.