Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change authentication type from email to username? #540

Closed
Pitu opened this issue Feb 26, 2016 · 11 comments
Closed

Change authentication type from email to username? #540

Pitu opened this issue Feb 26, 2016 · 11 comments

Comments

@Pitu
Copy link

Pitu commented Feb 26, 2016

Hello folks, I've been reading up the issues and searching for the terms 'email', 'user' and 'username' to see if I found anything like this. Unfortunately there are some posts that may held some info but those are for older versions of this package that worked with Laravel 4.

Im running Laravel 5.1.* and jwt-auth 0.5.*, and basically what I'm trying to do is after modifying the relevant user table on the DB, to make it possible to log with username instead of email. In my current setup I need the email column for something else than login, so I was hoping someone could point the right way to go about this.

Best regards

@intrepido
Copy link

I have the same problem, I need the authentication by username not email. Is there any way?

@anasceym
Copy link

anasceym commented Mar 7, 2016

Yes. There is a way, you can create a user token using JWTAuth facade credentials() method. Here is the workaround.

use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class AuthenticateController extends Controller
{
    public function authenticate(Request $request)
    {
        // grab credentials from the request
        //$credentials = $request->only('email', 'password');
        // Change email to username
        $credentials = $request->only('username', 'password');

        try {
            // attempt to verify the credentials and create a token for the user
            if (! $token = JWTAuth::attempt($credentials)) {
                return response()->json(['error' => 'invalid_credentials'], 401);
            }
        } catch (JWTException $e) {
            // something went wrong whilst attempting to encode the token
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        // all good so return the token
        return response()->json(compact('token'));
    }
}

@Pitu
Copy link
Author

Pitu commented Mar 7, 2016

Thank you @anasceym , that's a beautiful solution.

@Pitu Pitu closed this as completed Mar 7, 2016
@vlafranca
Copy link

Hi,
I know this is a old thread but there is an attribute in the config.php file to handle this :

/*
|--------------------------------------------------------------------------
| User identifier
|--------------------------------------------------------------------------
|
| Specify a unique property of the user that will be added as the 'sub'
| claim of the token payload.
|
*/

'identifier' => 'mail',

@pankajkachhwaye
Copy link

its right but how to set credential for both email or user name

@miczed
Copy link

miczed commented Jan 16, 2018

@pankajkachhwaye after a couple of hours I finally found a solution to make JWT auth work with username or email:

use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class AuthenticateController extends Controller
{
    public function authenticate(Request $request)
    {
        // assuming that the email or username is passed through a 'login' parameter
        $login = $request->input('login');
        $field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
        $request->merge([$field => $login]);
        $credentials = $request->only($field, 'password');

        try {
            // attempt to verify the credentials and create a token for the user
            if (! $token = JWTAuth::attempt($credentials)) {
                return response()->json(['error' => 'invalid_credentials'], 401);
            }
        } catch (JWTException $e) {
            // something went wrong whilst attempting to encode the token
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        // all good so return the token
        return response()->json(compact('token'));
    }
}

@kumardinkar9
Copy link

kumardinkar9 commented Feb 13, 2018

if there is need to login with username or email:

use JWTAuth;
use JWTAuthException;
class UserController extends Controller
{   
    public function login(Request $request){
        // Request input contains username, email and password
        if ($request->has(['username', 'email', 'password'])) {
            $credentials = $request->only('email', 'password');
        } // Request input contains username and password
        elseif ($request->has(['username', 'password'])) {
            $credentials = $request->only('username', 'password');
        } // Request input contains email and password
        elseif ($request->has(['email', 'password'])) {
            $credentials = $request->only('email', 'password');
        }
        else {
            $credentials = $request->only('email', 'password');
        }
        
        try {
           if (!$token = JWTAuth::attempt($credentials)) {
            return response()->json(['invalid_email_or_password']);
           }
        } catch (JWTAuthException $e) {
            return response()->json(['failed_to_create_token'], 500);
        }
        return response()->json(compact('token'));
    }
}

@lucasres
Copy link

I try all but when my credentials is username and password dont work :(

Any help?

@guilherme90
Copy link

@mandala21 i have same problem. =/

@specialtactics
Copy link

specialtactics commented Jan 3, 2020

If you want to do that you actually need to use an associative array when you pass it to the attempt() function, it doesn't magically know which field you want to find the user by.

Have a look at Illuminate\Auth\EloquentUserProvider::retrieveByCredentials()

You want to pass it this:

$credentials = ['username' => $request->input('username'), 'password' => $request->input('password')];

...
JWTAuth::attempt($credentials);

You can form the credentials array differently based on whatever circumstance you desire, just specify the database field names of the values you want to match against, in the user table.

@avinashseth
Copy link

Hi, i would like to check certain conditions also before letting user it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests