-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Comments
I have the same problem, I need the authentication by username not email. Is there any way? |
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'));
}
} |
Thank you @anasceym , that's a beautiful solution. |
Hi,
|
its right but how to set credential for both email or user name |
@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'));
}
} |
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'));
}
} |
I try all but when my credentials is username and password dont work :( Any help? |
@mandala21 i have same problem. =/ |
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. |
Hi, i would like to check certain conditions also before letting user it! |
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
The text was updated successfully, but these errors were encountered: