Trying to get Laravel multiple authentication to work using Doctrine instead of Eloquent. I've tried multiple things but keep getting stuck. I currently have two guards defined, two models, two login controllers, etc. If I enable either one or the other, they work. If I try both at the same time, only the default guard seems to work. When I try to access the other guard, I get redirected to the wrong login page.
If I go to /login - works as expected
If I go to /home (without being logged in) - redirected to /login as expected
If I go to /register- works as expected
If I go to /admin/login - works as expected
If I go to /admin/register - works as expected
If I go to /admin (without being logged in) - fail - should get redirected to /admin/login but instead getting redirected to /login
I'm sure I'm missing something simple. Everything works individually. It's just getting the /admin route to use the right middleware...I think...maybe?
My routes file:
Auth::routes();
// staff authentication routes
Route::group( [ 'middleware' => [ 'web' ] ], function() {
Route::get( 'admin/login', 'Auth\StaffLoginController#showLoginForm' );
Route::post( 'admin/login', [ 'as' => 'staff.login', 'uses' => 'Auth\StaffLoginController#login' ] );
Route::get( 'admin/register', 'Auth\StaffRegisterController#showRegistrationForm' );
Route::post( 'admin/register', [ 'as' => 'staff.register', 'uses' => 'Auth\StaffRegisterController#register' ] );
Route::group( [ 'middleware' => [ 'staff' ] ], function() {
Route::get( '/admin', 'AdminController#index' )->name( 'admin' );
});
});
Route::get('/home', 'HomeController#index')->name('home');
I've tried various route definitions but this is the latest iteration. None of the previous iterations worked either.
My auth file:
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'staff' => [
'driver' => 'session',
'provider' => 'adminusers',
]
],
'providers' => [
'users' => [
'driver' => 'doctrine',
'model' => App\Users\Customer::class,
],
'adminusers' => [
'driver' => 'doctrine',
'model' => App\Users\Staff::class,
]
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'adminusers' => [
'provider' => 'adminusers',
'table' => 'password_resets',
'expire' => 30,
],
],
My LoginController (basically same as out of the box):
class LoginController extends Controller {
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct() {
$this->middleware('guest')->except('logout');
}
}
My StaffLoginController:
<?php
class StaffLoginController extends Controller {
use AuthenticatesUsers;
protected $redirectTo = '/admin';
protected $guard = 'staff';
public function __construct() {
$this->middleware( 'guest' )->except( 'logout' );
}
public function showLoginForm() {
return view( 'auth.staff.login' );
}
public function login( Request $request ) {
$this->validate( $request, [
'email' => 'required|email',
'password' => 'required',
]);
if( auth()->guard( 'staff' )->attempt( [
'email' => $request->input( 'email' ),
'password' => $request->input( 'password' ),
])) {
return view( 'staff' );
} else {
return view( 'auth.staff.login' )->withErrors( [ 'email' => 'Authentication failed' ] );
}
}
protected function guard() {
return \Auth::guard( 'staff' );
}
}
My AdminController:
class AdminController extends Controller {
public function __construct() {
$this->middleware( 'auth:staff' );
}
public function index() {
return view( 'staff' );
}
}
My RedirectIfStaffUnauthenticated middleware (which is registered in Http\Kernel.php routeMiddleware as 'staff' => \SNJ\Http\Middleware\RedirectIfStaffUnauthenticated::class, ):
class RedirectIfStaffUnauthenticated {
public function handle( $request, Closure $next, $guard = 'staff' ) {
if ( !Auth::guard( $guard )->check() ) {
return view( 'auth.staff.login' );
}
return $next( $request );
}
}
UPDATE:
Changed routes in web.php to be as thus (removed the middleware from the admin/login and admin/register routes:
Auth::routes();
// staff authentication routes
Route::get( 'admin/login', 'Auth\StaffLoginController#showLoginForm' );
Route::post( 'admin/login', [ 'as' => 'staff.login', 'uses' => 'Auth\StaffLoginController#login' ] );
Route::get( 'admin/register', 'Auth\StaffRegisterController#showRegistrationForm' );
Route::post( 'admin/register', [ 'as' => 'staff.register', 'uses' => 'Auth\StaffRegisterController#register' ] );
Route::group( [ 'middleware' => [ 'staff' ] ], function() {
Route::get( '/admin', 'AdminController#index' )->name( 'admin' );
});
Route::get('/home', 'HomeController#index')->name('home');
No change. Still doesn't work.
Tried changing routes thusly (put all admin routes into 'staff' middleware:
Auth::routes();
// staff authentication routes
Route::group( [ 'middleware' => [ 'staff' ] ], function() {
Route::get( 'admin/login', 'Auth\StaffLoginController#showLoginForm' );
Route::post( 'admin/login', [ 'as' => 'staff.login', 'uses' => 'Auth\StaffLoginController#login' ] );
Route::get( 'admin/register', 'Auth\StaffRegisterController#showRegistrationForm' );
Route::post( 'admin/register', [ 'as' => 'staff.register', 'uses' => 'Auth\StaffRegisterController#register' ] );
Route::get( '/admin', 'AdminController#index' )->name( 'admin' );
});
Route::get('/home', 'HomeController#index')->name('home');
Same same. Still doesn't work.
Change your StaffLoginController to this,
<?php
class StaffLoginController extends Controller {
use AuthenticatesUsers;
public function showLoginForm() {
return view( 'auth.staff.login' );
}
public function login( Request $request ) {
$this->validate( $request, [
'email' => 'required|email',
'password' => 'required',
]);
if( auth()->guard( 'staff' )->attempt( [
'email' => $request->input( 'email' ),
'password' => $request->input( 'password' ),
])) {
return view( 'staff' );
} else {
return view( 'auth.staff.login' )->withErrors( [ 'email' => 'Authentication failed' ] );
}
}
}
remove constructor from AdminController, we are later going to call this middleware on routes.
class AdminController extends Controller {
public function index() {
return view( 'staff' );
}
}
You don't need to pass the guard value to auth middleware since you already defined as second middleware for authenticating admin routes.
update your staff middleware like this.
class RedirectIfStaffUnauthenticated {
public function handle( $request, Closure $next, $guard = null ) {
if ( Auth::guard( $guard )->check() ) {
if(! $guard == 'staff')
{
return redirect()->route( 'staff.login.form' );
}
}else return redirect()->route( 'staff.login.form' );
return $next( $request );
}
}
Now update your routes.
Route::get( 'admin/login', 'Auth\StaffLoginController#showLoginForm' );
Route::post( 'admin/login', [ 'as' => 'staff.login.submit', 'uses' => 'Auth\StaffLoginController#login' ] );
Route::get( 'admin/register', 'Auth\StaffRegisterController#showRegistrationForm' );
Route::post( 'admin/register', [ 'as' => 'staff.register.submit', 'uses' => 'Auth\StaffRegisterController#register' ] );
Route::group( [ 'middleware' => [ 'staff' ] ], function() {
Route::get( '/admin', 'AdminController#index' )->name( 'admin' );
});
Also add the following lines to your unauthenticated function in Handler.php file in app\Exceptions
$guard = array_get($exception->guards(), 0);
switch ($guard) {
case 'staff':
$login = 'admin/login';
break;
case 'users':
$login = 'login';
break;
default:
$login = 'login';
break;
}
return redirect()->guest(route('login'));
Please check your app/Kernel.php, you can see that guest is an alias for app/Http/Middleware/RedirectIfAuthenticated.php middleware.
You don't need to call constructors on both web.php file and on the controllers constructor.
Here the RedirectIfStaffUnauthenticated middleware checks the root /admin is authenticated and it has a guard value of staff, if not it will redirect to the /admin/login route.
Please read laravel doc for clarification
Hope this helps.
Related
I am currently learning Laravel and using Sanctum to perform authentication.
I have a route working /register and /login and I am trying to create /me endpoint that's protected using auth:sanctum which as a test just returns the authenticated user.
In my api.php I have the following:
Route::post('/auth/register', [UserController::class, "register"]);
Route::post('/auth/login', [UserController::class, "login"]);
Route::middleware('auth:sanctum')->get('/me', function(){
return auth()->user();
});
In my UserController class I have the following:
class UserController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function register(Request $request)
{
$user = User::create([
'name' => $request['name'],
'email' => $request['email'],
'password' => bcrypt($request['password'])
]);
return response([
'success' => $user->createToken('API Token')->plainTextToken
]);
}
public function login(Request $request)
{
$attr = $request->validate([
'email' => 'required|string|email|',
'password' => 'required|string|min:6'
]);
if (!Auth::attempt($attr))
{
return response('Credentials not found', 401);
}
return response([
'token' => auth()->user()->createToken('API Token')->plainTextToken
]);
}
public function logout()
{
auth()->user()->tokens()->delete();
return [
'message' => 'Tokens Revoked'
];
}
}
The /login and /register routes work fine, however, when I attempt to use the /logout or /me route which is using auth:sanctum middleware, I get the following error:
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [login] not defined.
Everything I've Google'd seem to show that I've implemented it correctly, so I'm not sure what I'm missing.
I managed to figure out the problem with some help from #LessMore.
I think most of the problem the auth.php being wrong. Under config/auth.php, under the api section change the driver from token to session, so it should be as follows:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'session',
'provider' => 'users',
'hash' => false,
],
],
The other thing was I was forgetting to add the Authorization header with the bearer token that is returned on the login and to put Accept application/json header.
Problem Statement:
I'm trying modify Multiple Authentication to authenticate different user by using Multiple Models, route, guards and Controller.
The user is redirected to teacher/home instead of v2/teacher/home
teacher/home is earlier route whereas v2/teacher/home is new modified route.
Below is Directory Structure
- app/
- Http/
- Controllers/
- Teacher/
- LoginController.php
- RegisterController.php
- Student/
- LoginController.php
- RegisterController.php
- Middleware/
- RedirectIfTeacher.php
- RedirectIfStudent.php
- RedirectIfNotTeacher.php
- RedirectIfNotStudent.php
- Models/
- Teacher.php
- Student.php
Files & Configuration
I will share the steps and code here to find where the problem is:
I created the necessary guards, providers and password brokers
I created the custom model
I created the custom middlewares
I created the necessary routes and controllers
App\Http\Controllers\v2\Teacher\Auth\LoginController.php
use AuthenticatesUsers, LogsoutGuard {
LogsoutGuard::logout insteadof AuthenticatesUsers;
}
public $redirectTo = '/v2/teacher/home';
public function __construct()
{
$this->middleware('teacher.guest', ['except' => 'logout']);
}
public function showLoginForm()
{
return view('v2.teacher.auth.login');
}
protected function guard()
{
return Auth::guard('teacher');
}
routes\web.php
Route::group(['prefix' => 'v2', 'as' => 'v2.'], function (){
Route::group(['prefix' => 'teacher', 'as' => 'teacher.'], function (){
Route::get('login', 'v2\Teacher\Auth\LoginController#showLoginForm')->name('login');
Route::post('login', 'v2\Teacher\Auth\LoginController#login')->name('login.post');
Route::group(['middleware' => 'auth:teacher'], function (){
Route::get('home', 'v2\Teacher\HomeController#index')->name('home');
app\Http\Kernel.php
protected $routeMiddleware = [
'check' => \App\Http\Middleware\RedirectIfNotCheck::class,
'check.guest' => \App\Http\Middleware\RedirectIfCheck::class,
'teacher' => \App\Http\Middleware\RedirectIfTeacher::class,
'teacher.guest' => \App\Http\Middleware\RedirectIfNotTeacher::class,
'student' => \App\Http\Middleware\RedirectIfStudent::class,
'student.guest' => \App\Http\Middleware\RedirectIfNotStudent::class,
App\Http\Middleware\RedirectIfTeacher.php
public function handle($request, Closure $next, $guard = 'teacher')
{
if (Auth::guard($guard)->check()) {
return redirect('v2/teacher/home');
}
return $next($request);
}
Auth::guard($guard)->check() returning false
config\auth.php
'guards' => [
'teacher' => [
'driver' => 'session',
'provider' => 'teachers',
],
'student' => [
'driver' => 'session',
'provider' => 'students',
],
'providers' => [
'teacher' => [
'driver' => 'eloquent',
'model' => App\Models\Teacher::class,
],
'student' => [
'driver' => 'eloquent',
'model' => App\Models\Student::class,
],
I am using...
Laravel 5.4
tymon/jwt-auth : 1.0.0-rc.2
I have application with two authentications API one is customers and the other is drivers each one has it's own table.
now let me describe shortly JWT package installation and the updates I did on it.
I installed the package as described in the JWT documents exactly.
Now comes to the quick start here I updated two Models one is the User and the second Driver.
Comes here to the Configure Auth guard again I used the configuration for the two guards let me show a snapshot of my auth.php.
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
'driver' => [
'driver' => 'session',
'provider' => 'drivers',
],
'driver-api' => [
'driver' => 'jwt',
'provider' => 'drivers',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'drivers' => [
'driver' => 'eloquent',
'model' => App\Models\Driver::class,
],
],
Now continue the application with authentication routes here is my Routes for the two Models
Here is the User and Driver Routes
Route::group( [
'prefix' => 'auth',
'middleware' => 'api'
], function () {
.......
});
Route::group( [
'prefix' => 'driver',
'middleware' => 'api'
], function () {
.......
});
Now comes the AuthController
in the JWT documentation the construct is writing like that.
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
I found some article that suggest to make it something like this to switch between the two models we have.
so here with my controller looks like now.
public function __construct() {
$this->user = new User;
$this->driver = new Driver;
}
public function userLogin( Request $request ) {
Config::set( 'jwt.user', 'App\Models\User' );
Config::set( 'auth.providers.users.model', User::class );
$credentials = $request->only( 'email', 'password' );
$token = null;
try {
if ( $token = $this->guard()->attempt( $credentials ) ) {
return response()->json( [
'response' => 'error',
'message' => 'invalid_email_or_password',
] );
}
} catch ( JWTAuthException $e ) {
return response()->json( [
'response' => 'error',
'message' => 'failed_to_create_token',
] );
}
return response()->json( [
'response' => 'success',
'result' => [
'token' => $token,
'message' => 'I am front user',
],
] );
}
public function driverLogin( Request $request ) {
Config::set( 'jwt.user', 'App\Models\Driver' );
Config::set( 'auth.providers.users.model', Driver::class );
$credentials = $request->only( 'email', 'password' );
$token = null;
try {
if ( ! $token = $this->guard()->attempt( $credentials ) ) {
return response()->json( [
'response' => 'error',
'message' => 'invalid_email_or_password',
] );
}
} catch ( JWTAuthException $e ) {
return response()->json( [
'response' => 'error',
'message' => 'failed_to_create_token',
] );
}
return response()->json( [
'response' => 'success',
'result' => [
'token' => $token,
'message' => 'I am driver user',
],
] );
}
public function me() {
return response()->json( $this->guard()->user() );
}
public function logout() {
$this->guard()->logout();
return response()->json( [ 'message' => 'Successfully logged out' ] );
}
public function refresh() {
return $this->respondWithToken( $this->guard()->refresh() );
}
protected function respondWithToken( $token ) {
return response()->json( [
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => $this->guard()->factory()->getTTL() * 60
] );
}
public function guard() {
return Auth::guard();
}
Now with is happening and the problems I faced
Now the driver api is working as login only Ex.
localhost:8000/api/driver/login Working fine
but when try to get the driver user id like this
localhost:8000/api/driver/me it return empty array
Second Issue comes.
the use login from the interface for Ex. http://localhost:8000/login it returns back to the login screen without any errors becouse the login information is right but the defaults in the auth.php is 'guard'=>'api' if I change it to 'guard'=>'web' it do the login correctly.
even the User API for Ex. localhost:8000/api/auth/login always return
{
"response": "error",
"message": "invalid_email_or_password"
}
Update
I solved half the way
I updated the AuthController to be something like this.
public function __construct() {
if ( Request()->url() == '/api/driver/me' ) {
$this->middleware( 'auth:driver-api', [ 'except' => [ 'login' ] ] );
} elseif ( Request()->url() == '/api/customer/me' ) {
$this->middleware( 'auth:api', [ 'except' => [ 'login' ] ] );
}
}
and the login function to be something like this.
public function login() {
if ( Request()->url() == '/api/driver' ) {
Config::set( 'auth.providers.users.model', Driver::class );
$credentials = request( [ 'email', 'password' ] );
if ( ! $token = auth()->attempt( $credentials ) ) {
return response()->json( [ 'error' => 'Unauthorized' ], 401 );
}
return $this->respondWithToken( $token );
}
Config::set( 'auth.providers.users.model', User::class );
$credentials = request( [ 'email', 'password' ] );
if ( ! $token = auth()->attempt( $credentials ) ) {
return response()->json( [ 'error' => 'Unauthorized' ], 401 );
}
return $this->respondWithToken( $token );
}
but still have problem in auth.php here it is
'defaults' => [
'guard' => 'driver-api',
'passwords' => 'users',
],
here I need to switch the 'guard'=>'api' to be 'guard'=>'driver-api' in case if URL request is localhost:8000/api/driver/login and 'guard'=>'api' in case if URL request is localhost:8000/api/customer/login any way to do this.
Update 2
Here is the driver Model
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Driver extends Authenticatable implements JWTSubject {
protected $guard = 'driver';
protected $fillable = [
...
'email',
'password',
...
];
protected $hidden = [
'password',
'remember_token',
];
public function getJWTIdentifier() {
return $this->getKey();
}
public function getJWTCustomClaims() {
return [];
}
}
and the User Model
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject {
use Notifiable;
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
public function getJWTIdentifier() {
return $this->getKey();
}
public function getJWTCustomClaims() {
return [];
}
I need some help, Ideas please.
There is no need to change the providers in config/auth.php.
You can change the __construct function in each of your controllers as follows. So that jwt know which model to authenticate.
DriverController
function __construct()
{
Config::set('jwt.user', Driver::class);
Config::set('auth.providers', ['users' => [
'driver' => 'eloquent',
'model' => Driver::class,
]]);
}
My example when i used multi auth with jwt
I have 2 models :
1. users
2. admins
the routes :
Route::post('auth/userlogin', 'ApiController#userLogin');
Route::post('auth/adminlogin', 'ApiController#adminLogin');
the controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Config;
use JWTAuth;
use JWTAuthException;
use App\User;
use App\Admin;
class ApiController extends Controller
{
public function __construct()
{
$this->user = new User;
$this->admin = new Admin;
}
public function userLogin(Request $request){
Config::set('jwt.user', 'App\User');
Config::set('auth.providers.users.model', \App\User::class);
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json([
'response' => 'error',
'message' => 'invalid_email_or_password',
]);
}
} catch (JWTAuthException $e) {
return response()->json([
'response' => 'error',
'message' => 'failed_to_create_token',
]);
}
return response()->json([
'response' => 'success',
'result' => [
'token' => $token,
'message' => 'I am front user',
],
]);
}
public function adminLogin(Request $request){
Config::set('jwt.user', 'App\Admin');
Config::set('auth.providers.users.model', \App\Admin::class);
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json([
'response' => 'error',
'message' => 'invalid_email_or_password',
]);
}
} catch (JWTAuthException $e) {
return response()->json([
'response' => 'error',
'message' => 'failed_to_create_token',
]);
}
return response()->json([
'response' => 'success',
'result' => [
'token' => $token,
'message' => 'I am Admin user',
],
]);
}
}
I hope that's help you .
First let me thank you #AmrAbdelRahman for you efforts and your time.
My problem was the application always using my default authentication "guard" as my default looks like that
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
so every time I try to authenticate the other user which was the driver it fails during the default authenticate is api and the it should using driver in this case.
what I did in my case was making a switcher in my App\Providers\AppServiceProvider under the boot here is how it looks like
$this->app['router']->matched(function (\Illuminate\Routing\Events\RouteMatched $e) {
$route = $e->route;
if (!array_has($route->getAction(), 'guard')) {
return;
}
$routeGuard = array_get($route->getAction(), 'guard');
$this->app['auth']->resolveUsersUsing(function ($guard = null) use ($routeGuard) {
return $this->app['auth']->guard($routeGuard)->user();
});
$this->app['auth']->setDefaultDriver($routeGuard);
});
Now in my case if the $guard =api it will read that guard and act correctly and if it's driver it will use the new guard and act as expected. Hope this will help some one in future.
I am somewhat new to Laravel.
I have created a form, submitted it for authorisation but then I am told (by Firefox) the routing will never complete. I know the login has worked as I intercepted it.
Here is my routes.php:
Route::get('/',function()
{
return view('welcome');
})->name('home');
Route::get('/welcome', function () {
return view('welcome');
});
Route::post('/signin',
[
'uses' =>'UserController#postSignIn',
'as' => 'SignIn'
]);
Route::get('/dashboard',
[
'uses' => 'UserController#getDashboard',
'as' => 'DashBoard',
'middleware' => 'auth'
]);
Route::get('/logout',
[
'uses' => 'UserController#getLogout',
'as' => 'Logout'
]);
and here is the UserController:
class UserController extends Controller
{
public function postSignIn(Request $request)
{
$this->validate($request,
[
'email' => 'required | email',
'password' => 'required'
]);
if (Auth::attempt([ 'email' => $request['email'], 'password' =>$request['password'] ]) )
{
//exit("authorised");
$message = "you are now logged in";
return redirect()->route('DashBoard')->with(['successmessage' =>$message]);
}
else
{
$message = "username\password combination not correct";
//exit('not - email = '.$request['email'].' password = '. $request['password']);
return redirect()->back()->with(['errormessage' => $message] );
}
}
public function getLogout()
{
Auth::logout();
return redirect()->route('home');
}
public function getDashboard()
{
return redirect()->route('DashBoard');
}
}
As can be seen by what is commented out the authorisation is OK
But I get this from Firefox
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
Just use to() in your return as
return redirect()->to('DashBoard')->with(['successmessage' =>$message]);
add this to your route
Route::get('/signin',
[
'uses' =>'UserController#postSignIn',
'as' => 'SignIn'
]);
I get this error when i try to access the post signin route. I'm new to laravel and i can't seem to figure out how to solve this error. Please help.
My Routes.php
Route::group(['middleware' => ['web']], function () {
Route::get('/', [
'uses'=>'\ocsaf\Http\Controllers\HomeController#index',
'as'=>'home',
]);
/*
*Authentication
*/
Route::get('/signup', [
'uses'=>'\ocsaf\Http\Controllers\AuthController#getSignUp',
'as'=>'auth.signup',
'middleware' => ['guest'],
]);
Route::post('/signup', [
'uses'=>'\ocsaf\Http\Controllers\AuthController#postSignUp',
'middleware' => ['guest'],
]);
Route::get('/signin', [
'uses'=>'\ocsaf\Http\Controllers\AuthController#getSignIn',
'as'=>'auth.signin',
'middleware' => ['guest'],
]);
Route::post('/signup', [
'uses'=>'\ocsaf\Http\Controllers\AuthController#postSignIn',
'middleware' => ['guest'],
]);
Route::get('/signout', [
'uses'=>'\ocsaf\Http\Controllers\AuthController#getSignOut',
'as'=>'auth.signout',
]);
/*
*search
*/
Route::get('/search', [
'uses'=>'\ocsaf\Http\Controllers\SearchController#getResults',
'as'=>'search.results',
]);
/*
*Profile
*/
Route::get('/user/{username}', [
'uses'=>'\ocsaf\Http\Controllers\ProfileController#getProfile',
'as'=>'profile.index',
]);
Route::get('/profile/edit', [
'uses'=>'\ocsaf\Http\Controllers\ProfileController#getEdit',
'as'=>'profile.edit',
'middleware'=>['auth'],
]);
Route::post('/profile/edit', [
'uses'=>'\ocsaf\Http\Controllers\ProfileController#postEdit',
'as'=>'profile.edit',
'middleware'=>['auth'],
]);
Route::post('/profile/edit', [
'uses'=>'\ocsaf\Http\Controllers\StatusController#postStatus',
'as'=>'status.post',
'middleware'=>['auth'],
]);
});
AuthController.php
namespace ocsaf\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use ocsaf\Models\User;
class AuthController extends Controller
{
public function getSignUp()
{
return view('auth.signup');
}
public function postSignUp(Request $request)
{
$this->validate($request, [
'email' => 'required|unique:users|email|max:255',
'username' => 'required|unique:users|alpha_dash|max:255',
'password' => 'required|min:6',
]);
User::create([
'email' => $request-> input('email'),
'username' => $request-> input('username'),
'password' => bcrypt($request -> input('password')),
]);
return redirect()
->route('home')
->with('info', 'You have signed up, Please sign in!');
}
public function getSignIn()
{
return view('auth.signin');
}
public function postSignIn(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
if(!Auth::attempt($request -> only(['email', 'password' ]),
$request -> has('remember'))){
return redirect() ->back()->
with('info', 'could not sign you in with those details ');
}
return redirect() ->route('home')->with('info', 'You are now signed in');
}
}
my signin.blade.php form statement
<form class="form-vertical" role = "form"
method = "post" action = "{{ route('auth.signin'); }}">
Your form method is post but for the route auth.signin the HTTP verb is get.