I'm new to Laravel 5.6 and are trying to write an API with the public route /signup.
For JWT auth, I'm using jwt-auth 1.0.0-rc.2.
routes/api.php
// This code WORKS, the route is public
Route::middleware('guest:api')->get('/signup', function(Request $request) {
return "Sign up"; // This code belongs in the controller
});
// This code DOES NOT WORK, authentification needed
Route::get('signup', 'AuthController#signup')->middleware('guest');
// This code DOES NOT WORK
Route::group([
'middleware' => 'guest:api'
], function($router) {
Route::get('signup', 'AuthController#signup');
});
I couldn't find anything useful in the docs, but it should be my wrong way of adding the guest permission, as the first example works.
How can I make the the not working code work? Any idea? Thanks!
Route::middleware(['guest:api'])->group(function () {
Route::get('signup', 'AuthController#signup');
Route::get('mySecondRoute', 'AuthController#mySecondFunction');
});
or even just
Route::get('signup', 'AuthController#signup');
out of any group
Related
I'm trying to use simple laravel api for getting and sending requests, after define this api routes in api.php:
Route::prefix('Api/v1')->group(function () {
Route::any('login', 'Api\v1\AuthController#login');
Route::any('register', 'Api\v1\AuthController#register');
});
and creating AuthController in app/http/controller/Api/v1 directory:
class AuthController extends Controller
{
public function login()
{
dd(request()->all());
}
public function register()
{
dd(request()->all());
}
}
i get 404 error on this link:
http://127.0.0.1:8000/Api/v1/login
how can i resolve this problem?
Routes in api.php are automatically prefixed with /api. Currently, your routes are:
http://127.0.0.1:8000/api/Api/v1/login
http://127.0.0.1:8000/api/Api/v1/register
So navigating to http://127.0.0.1:8000/Api/v1/login is a 404.
If you remove /Api, and just use Route::prefix('/v1') ... then you should have no issue.
Also, always double check your routes with php artisan route:list to see what's wrong.
The API Routes are already prefixed by /api . I think the correct structure you'd looking for would be
Route::prefix('v1')->group(function () {
Route::any('login', 'AuthController#login');
Route::any('register', 'AuthController#register');
});
This way, you're calling the methods Login and Register from you /Controllers/AuthController file with the route
http://127.0.0.1:8000/api/v1/login
You can use many ways to define routes for API in laraval > routes > api.php file.
In this i'm going to explain how we can use routes group in the laraval..
Route::group([
'namespace' => 'Customers', //namespace App\Http\Controllers\Customers;
'middleware' => 'auth:api', // this is for check user is logged in or authenticated user
'prefix' => 'customers' // you can use custom prefix for your rote {{host}}/api/customers/
], function ($router) {
// add and delete customer groups
Route::get('/', [CustomerController::class, 'index']); // {{host}}/api/customers/ this is called to index method in CustomerController.php
Route::post('/create', [CustomerController::class, 'create']); // {{host}}/api/customers/create this is called to create method in CustomerController.php
Route::post('/show/{id}', [CustomerController::class, 'show']); // {{host}}/api/customers/show/10 this is called to show method in CustomerController.php parsing id to get single data
Route::post('/delete/{id}', [CustomerController::class, 'delete']); // {{host}}/api/customers/delete/10 this is called to delete method in CustomerController.php for delete single data
});
You can create controller using artisan command with default methods
php artisan make:controller Customers/CustomerController --resource
I'm looking to create a route group that enables particular users to view information on my site without being authenticated.
At the moment, i've created a route service provider called 'public' as follows:
Route::get('customer/application', function () {
return view('customerview.customer-application');
});
When I write 'php artisan route:list' the following comes up for the route:
Method: GET | HEAD
URL: customer/application
Middleware: ''
I have removed all middleware in an attempt to bypass auth, but with no luck.
The area which is redirecting me to the login page is here in App\Exceptions\Handler.php:
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest('login');
}
I also tried assigning the route to 'guest' group but no help. How can I bypass the return redirect()->guest('login'); for different groups?
Rookie mistake - I had a route with customer/{id} so I changed my new route to open/customer/application instead.
But for anyone wondering how to make a route outside of the scope of auth?
In RouteServiceProvider.php I created a mapping for the 'public' routes to enable me to create a new file with all my routes as so:
protected function mapPublicRoutes()
{
Route::group([
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/public.php');
});
}
In routes/public.php i added the following:
Route::get('open/customer/application', function () {
return view('customerview.customer-application');
});
Route::auth(); is still in my web.php file at the end without any problems / collision.
I hope my mistake will educate others to check their route files before bothering the Stackexchage community :')
I'm a beginner in Laravel and I'm having some difficulties with routes. Actually, I'm having difficulties with a lot of things in Laravel, some of which I've managed to wrap my head around (such as migrations, seeding and authentication) but this is one of the most basic ones.
I've been creating routes based on the one that comes with Laravel. However, after much googling, something seems off. I'm not sure this is how it should be done.
My current web.php file looks like this:
Route::get('/', function () {
return view('pages.home');
});
Route::get('/about', function () {
return view('pages.about');
});
Route::get('/login', function () {
return view('login');
});
Route::get('/student', function () {
return view('profiles.student');
});
Route::get('/professor', function () {
return view('profiles.prof');
});
Route::get('/profadmin', function () {
return view('profiles.profadmin');
});
Route::get('/ident', function () {
return view('pages.ident');
});
// Authentication
Auth::routes();
Route::post('/login', function () {
return view('pages.ident');
});
Route::get('logout', 'Auth\LoginController#logout');
// Route::get('/home', 'HomeController#index')->name('home');
// Route::get('/ident', 'HomeController#ident')->name('ident');
//
// Route::get('/aluno', 'HomeController#aluno')->name('aluno');
//
// Route::get('/ident', 'HomeController#ident')->name('ident');
Also, certain pages should only be viewed by authenticated users and I'm having a hard time understanding how exactly that is done and how the routes should reflect that.
I'm sorry if this is simple stuff, but this is my first time using a PHP framework. Any help will be much appreciated.
lets suppose you want to protect the about route
then in the web.php file, replace your about route with this:
Route::get('/about', function () {
return view('pages.about');
})->middleware('auth');
now anyone hits /about and not logged in, it will be redirected to /login
if you want to know more about authentication, Laravel documentation really the best place for you:
https://laravel.com/docs/5.5/authentication#protecting-routes
First if you are beginner you should read Laravel documentation & Laracasts
In your routes you trying to show only views
Route::get('/about', function () {
return view('pages.about');
});
In Laravel 5.6 you can do it like this
Route::view('/about', 'viewName');
I am doing project in laravel 5.2. For authentication, I am using API Token Authentication from Laravel 5.2. I have referred https://gistlog.co/JacobBennett/090369fbab0b31130b51 and my code looks like,
Routes.php
Route::post('login/','UserController#login');
Route::group(['prefix' => 'api/v1', 'middleware' => 'auth:api'], function () {
Route::get('/', function () {
return "Hi";
});
});
and in UserController.php I did simple login as follows,
public function login(Request $request){
$email = $request->input('email');
$user = DB::table('users')->where('email',$email)->first();
return $user->api_token;
}
I am using postman and first I did login and it returns me the correct api_token from table but when I try to access urls within middleware then it throws an error page as,
Sorry, the page you are looking for could not be found.
1/1
NotFoundHttpException in RouteCollection.php line 161:
I tried,
localhost:8888/ as well as
localhost:8888/api_token=6CnUsIKlmwHXYQNFAuhUTDweUe707gJU2nM2j1Kwjn80nFgmaJHGXuAdN3BX
But still it shows same error page.
Try to change your route / for this code in your Routes.php:
Route::post('login/','UserController#login');
Route::group(['prefix' => 'api/v1', 'middleware' => 'auth:api'], function () {
Route::get('/', array('as'=>'api_login', 'uses' => 'UserController#api_login'));
});
And in your UserController.php add:
public function api_login(){
return "Hi";
}
I use the Auth class, not an api and if i put the code in Routes.php it didn't work, but if I put the same code inside a Controller and in my Routes.php add the 'uses', it works.
I hope that it works for you too.
Good Luck!
If it helps anyone that googles this issue, I had the same problem and after hours of playing about with it realised it was because I had no api_tokens in my database, once I added it was fine, just weird behaviour that it gives 404 when api_token is not valid.
Hello!
I'm trying to learning laravel, and i started with doing a page that only logged people can access ( I don't have login yet ) , but i'm just testing, so far i have:
Routes.php:
<?php
Route::get('/', ['as' => 'index', function () {
return view('index');
}]);
Route::get('/home', function () {
return view('home');
});
Ok, this file contains the routes , i searched how to give a name to the route, so i can redirect user to that route when he don't have access.
My other file is home.blade.php:
<?php
if (!Auth::check()){
return Redirect::route('index');
}
?>
So is that the best way to check if user is logged? I gave a name to the route, index , so if user is not logged i want to redirect back to index, is not working tho, i want to know if this is the best option to achieve my goal.
Thank you.
The best way, as said at documentation is:
Route::get('/home', ['middleware' => 'auth', function () {
// here will be only authorized access
return view('home');
}]);
Auth middleware defined by default.
use Auth; loading facades
if (Auth::check()){
if(Auth::Guest()):
return Redirect::route('index');
endif;
}
?>