Skip to content

Commit

Permalink
Added reset password forgot password
Browse files Browse the repository at this point in the history
  • Loading branch information
johndavedecano committed Sep 15, 2018
1 parent 88203df commit 817cc0f
Show file tree
Hide file tree
Showing 19 changed files with 315 additions and 19 deletions.
5 changes: 3 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
APP_URL=http://localhost:8000
APP_SPA_URL=http://localhost:3000

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
Expand All @@ -21,7 +22,7 @@ REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(UserAuthService $authService)
$this->authService = $authService;
}

public function sendResetEmail(ForgotPasswordRequest $request)
public function forgot(ForgotPasswordRequest $request)
{
$this->authService->forgot($request->get('email'));

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function resetPassword(ResetPasswordRequest $request)
protected function credentials(ResetPasswordRequest $request)
{
return $request->only(
'email', 'password', 'password_confirmation', 'token'
'password', 'password_confirmation', 'token'
);
}
}
12 changes: 12 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace App\Models;

use App\Notifications\ForgotPassword;
use Hash;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand Down Expand Up @@ -131,4 +132,15 @@ public function getJWTCustomClaims()
{
return [];
}

/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ForgotPassword($token));
}
}
54 changes: 54 additions & 0 deletions app/Notifications/ForgotPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class ForgotPassword extends Notification
{
/**
* The password reset token.
*
* @var string
*/
public $token;

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

/**
* Get the notification's channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Build the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$url = url(config('app.spa_url').'/auth/reset/'.$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)
->line('If you did not request a password reset, no further action is required.');
}
}
2 changes: 1 addition & 1 deletion app/Services/User/UserAuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function reset($request = [])
throw new HttpException(500);
}

return $this->user->where('email', $request['email'])->first();
return $response;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App;

use App\Notifications\ForgotPassword;
use Hash;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand Down Expand Up @@ -59,4 +60,15 @@ public function getJWTCustomClaims()
{
return [];
}

/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ForgotPassword($token));
}
}
16 changes: 15 additions & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,21 @@
|
*/

'url' => env('APP_URL', 'http://localhost'),
'url' => env('APP_URL', 'http://localhost:8000'),


/*
|--------------------------------------------------------------------------
| Application SPA URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| your application so that it is used when running Artisan tasks.
|
*/

'spa_url' => env('APP_SPA_URL', 'http://localhost:3000'),

/*
|--------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion config/boilerplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
// here you can specify some validation rules for your password recovery procedure
'validation_rules' => [
'token' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed'
]
]
Expand Down
2 changes: 1 addition & 1 deletion resources/apps/frontend/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
NODE_PATH=src
APP_NAME=Laragym
APP_NAME=LARAGYM
APP_API_URL=http://localhost:8000
2 changes: 1 addition & 1 deletion resources/apps/frontend/src/components/Layouts/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Header extends React.Component {
<div>
<Navbar color="dark" dark expand="md">
<Link className="navbar-brand" to="/">
{process.env.APP_NAME || 'Laragym'}
{process.env.APP_NAME || 'LaraGym'}
</Link>
<NavbarToggler onClick={this.toggle} />
<Collapse isOpen={this.state.isOpen} navbar>
Expand Down
39 changes: 37 additions & 2 deletions resources/apps/frontend/src/contexts/auth-context.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import {login} from 'requests/auth';
import {login, forgot, reset} from 'requests/auth';
import getErrorMessage from 'utils/getErrorMessage';
import notify from 'utils/notify';
import initAxios from 'utils/initAxios';
Expand Down Expand Up @@ -69,7 +69,41 @@ class AuthProvider extends React.Component {
);
};

forgot() {}
reset = async (data = {}) => {
try {
await reset(data);
notify({
type: 'success',
text: 'Successfully logged in!',
});
setTimeout(() => {
window.location.replace('/auth/login');
});
} catch (error) {
notify({
type: 'error',
text: getErrorMessage(error),
});
}
};

forgot = async (data = {}) => {
try {
await forgot(data);
notify({
type: 'success',
text: 'Successfully logged in!',
});
setTimeout(() => {
window.location.replace('/auth/login');
});
} catch (error) {
notify({
type: 'error',
text: getErrorMessage(error),
});
}
};

render() {
const {user, token} = this.state;
Expand All @@ -82,6 +116,7 @@ class AuthProvider extends React.Component {
login: this.login,
logout: this.logout,
forgot: this.forgot,
reset: this.reset,
}}
>
{this.props.children}
Expand Down
68 changes: 65 additions & 3 deletions resources/apps/frontend/src/pages/auth/forgot.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,73 @@
import React, {Component} from 'react';
import {Link} from 'react-router-dom';
import {Button, Form, FormGroup, Label, Input} from 'reactstrap';

import withAuth from 'enhancers/withAuth';

class Forgot extends Component {
state = {};
state = {
isLoading: false,
};

isStillMounted = false;

componentDidMount() {
this.isStillMounted = true;
}

componentWillUnmount() {
this.isStillMounted = false;
}

onSubmit = async event => {
event.preventDefault();
this.setState({isLoading: true});

await this.props.forgot({
email: this.email.value,
});

this.isStillMounted && this.setState({isLoading: false});
};

render() {
return <div>sdgagsad</div>;
return (
<div className="card card-login mx-auto mt-5">
<div className="card-header text-center">Forgot Password</div>
<div className="card-body">
<Form onSubmit={this.onSubmit}>
<FormGroup>
<div className="form-label-group">
<Input
innerRef={email => (this.email = email)}
type="email"
id="inputEmail"
className="form-control"
placeholder="Email address"
autoFocus
disabled={this.state.isLoading}
/>
<Label for="inputEmail">Email address</Label>
</div>
</FormGroup>
<Button
disabled={this.state.isLoading}
size="lg"
color="primary"
block
>
{this.state.isLoading ? 'Please Wait...' : 'Recover'}
</Button>
</Form>
<div className="text-center mt-3">
<Link to="/auth/login" className="d-block small">
Back To Login
</Link>
</div>
</div>
</div>
);
}
}

export default Forgot;
export default withAuth(Forgot);
8 changes: 8 additions & 0 deletions resources/apps/frontend/src/pages/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@ export default {
loader: () => import('./forgot'),
}),
},
{
exact: true,
auth: false,
path: '/auth/reset/:token',
component: Loadable({
loader: () => import('./reset'),
}),
},
],
};
2 changes: 1 addition & 1 deletion resources/apps/frontend/src/pages/auth/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Login extends Component {
color="primary"
block
>
Login
{this.state.isLoading ? 'Please Wait...' : 'Login'}
</Button>
</Form>
<div className="text-center mt-3">
Expand Down
Loading

0 comments on commit 817cc0f

Please sign in to comment.