简体   繁体   中英

Login auth in Laravel without using database

Im somewhat new to laravel, i need to set an user/password login system, but i cannot use the default database. Is possible to store the information credentials other than in a database? What would an example look like?

Laravel's Authentication System relies on Eloquent and therefore on a database. With Sushi you can use an Array as your database driver. Maybe this would be an approach which could work for you.

Another idea could be to use Basic Authentication . You can set this up in your webserver (Apache, Nginx, etc.) or in Laravel with a Middleware.

Here's how such a middleware could look like.

namespace App\Http\Middleware;

use Closure;

class BasicAuthMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $authenticationHasPassed = false;

        if ($request->header('PHP_AUTH_USER', null) && $request->header('PHP_AUTH_PW', null)) {
            $username = $request->header('PHP_AUTH_USER');
            $password = $request->header('PHP_AUTH_PW');

            if ($username === config('auth.basic_auth.username') && $password === config('auth.basic_auth.password')) {
                $authenticationHasPassed = true;
            }
        }

        if ($authenticationHasPassed === false) {
            return response()->make('Invalid credentials.', 401, ['WWW-Authenticate' => 'Basic']);
        }

        return $next($request);
    }
}

(This code example is from a blog post of mine, where I needed an authentication feature without using a database).

I recommend reading the documentation about Middleware and Configuration if you want to implement this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM