This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

jasonwong
jasonwong

The document does not describe about the user authentication on the front-end, I used the Auth::check() and Auth::attempt() ie are good working in the Laravel framework, but it throws an exception Class 'Auth' not found when I called Auth::check() in October.

I reference the Rainlab\User plugins, but do not get the idea , is there any documents about of user authentication?

I like the October, because it makes my develop easier, but I can not understand of user authentication on front-end. is there anyone who can help me?

Thanks.

Last updated

ndcisiv
ndcisiv

Plugin Documentation

There is the plugin documentation available, should get you started.

jasonwong
jasonwong

@ndcisiv You may not understand what I mean, I do not encounter problems using the plugins, I want to achieve the foreground User Authentication without other plugins, I use Auth in my plugin, there throws an exception Class 'Auth' not found, I tried to use Auth;, the problem still not resolved, I want to know how to use the Auth such as in Laravel.

Thanks!

Last updated

ndcisiv
ndcisiv

I just added use Auth; to a plugin and tried a simple $this->page['whoami'] = Auth::getUser(); and it displayed the output with no problems. Perhaps you're trying to do something different, but it seems to work fine for me without modifying anything else.

jasonwong
jasonwong

@ndcisiv You tested plugin are Rainlab\User or use the newly created with youself?

Anand Patel
Anand Patel

@jasonwong

For using auth class in your plugin you have to register auth in plugin.php please check following links you get some ideas how that auth is working

https://github.com/rainlab/user-plugin/blob/master/Plugin.php#L25

https://github.com/rainlab/user-plugin/blob/master/facades/Auth.php

https://github.com/rainlab/user-plugin/blob/master/classes/AuthManager.php

Last updated

jasonwong
jasonwong

@Anand I got it, the rainlab\user plugin inherited user management logic of backend, I got the idea, Thanks you!!

leancoder
leancoder

@jasonwong So how exactly did you do it then? I am banging my head too and still do....

RomaldyMinaya
RomaldyMinaya

@jasonwong do you know how to fetch only current user's records, for example i have a table with clients, but when the user is viewing the table's data i want him to see only the records created by him.


The clients table have a user_id field.

As you can see, the clients and backend_users's relation are med by the user_id field.

Last updated

salma el hajraoui
salma el hajraoui

try use BackendAuth; $user = BackendAuth::getUser(); dd($user);

Meysam
Meysam

The following works for me:

use October\Rain\Database\ModelException;
use RainLab\User\Facades\Auth;

Route::post('user/login/{username}/{password}', ['as' => 'authentication', function ($username, $password) {
    try {
        $user = Auth::authenticate([
            'login' => $username,
            'password' => $password
        ]);
        return sprintf('welcome %s!!!', $username);
    }
    catch (\October\Rain\Auth\AuthException $e) {
        return sprintf('Invalid credentials!');
    }
}]);

And for registration:

Route::post('user/register/{name}/{email}/{password}', ['as' => 'registration', function ($name, $email, $password) {
    try {
        $user = Auth::register([
            'name' => $name,
            'email' => $email,
            'password' => $password,
            'password_confirmation' => $password,
        ]);
        return response()->json([
            'success' => true,
            'user' => [
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email
            ]
        ]);
    }
    catch (ModelException $e) {
        return [
            'result' => false,
            'reason' => $e->getMessage()
        ];
    }
}]);

I hope this helps :)

TerjeN
TerjeN

Meysam said:

The following works for me:

use October\Rain\Database\ModelException; use RainLab\User\Facades\Auth;

Route::post('user/login/{username}/{password}', ['as' => 'authentication', function ($username, $password) { try { $user = Auth::authenticate([ 'login' => $username, 'password' => $password ]); return sprintf('welcome %s!!!', $username); } catch (\October\Rain\Auth\AuthException $e) { return sprintf('Invalid credentials!'); } }]);

And for registration:

Route::post('user/register/{name}/{email}/{password}', ['as' => 'registration', function ($name, $email, $password) { try { $user = Auth::register([ 'name' => $name, 'email' => $email, 'password' => $password, 'password_confirmation' => $password, ]); return response()->json([ 'success' => true, 'user' => [ 'id' => $user->id, 'name' => $user->name, 'email' => $user->email ] ]); } catch (ModelException $e) { return [ 'result' => false, 'reason' => $e->getMessage() ]; } }]);

I hope this helps :)

Thank you for this, works perfect for my Octorber+Angular app.

neilcarpenter
neilcarpenter

I'm using Rainlab user plugin on a project and I've got a routes.php in my plugin with an entry for Route::get('/download/{file}', 'Impelling\School\Controllers\DownloadsController@download'); in my download method on downloadscontroller I want to do Auth::check() but it returns false even though i'm logged in - can I not use the Auth facade this way?

I've tried with use Auth; and use RainLab\User\Facades\Auth;

But i still get the same outcome.

Last updated

Meysam
Meysam

No, Auth::check() won't work in routes because there is no session available there. You have to authenticate users before allowing them to access routes. Consider using https://laravel.com/docs/5.5/passport

There is a plugin for passport which is not still complete: https://github.com/LukeTowers/oc-passport-plugin

I have implemented my own mechanism to authenticate users and give access_token to them. Then this access_token is used in requests sent to routes.

read this: https://auth0.com/learn/refresh-tokens/

1-14 of 14

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.