When I register a member at Laravel, I send a confirmation email. But when I click on the verification link, the column in the database does not fill.
Sample codes:
protected function create(array $data)
{
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone' => $data['phone'],
'password' => Hash::make($data['password']),
]);
$user->sendEmailVerificationNotification();
return $user;
}
public function register(Request $request) {
$validation = $this->validator($request->all());
if($request->input('type') == "customer" || $request->input('type') == "company") {
if ($validation->fails()) {
return response()->json([$validation->errors()->toArray()], 400);
}else {
$this->create($request->all());
}
}else {
return response()->json([
0 => ['Üyelik sırasında bir hata oluştu. Lütfen tekrar deneyiniz.']
], 404);
}
}
1) Have you migrated the tables after running the auth boilerplate?
2) Have you checked your logs for any additional errors that might be showing?
Related
When I register a new user and I want to sign him in by using auth attempt it doesn't work while the user is saved to database
static function register()
{
if(self::$validate['message'])
{
$user = User::create([
'name' => self::$values['name'],
'email' => self::$values['email'],
'password' => Hash::make(self::$values['password'])
]);
Auth::attempt($user,true);
Auth::attempt($user->only(['email','password']));
return result::repsonse(true);
} else
return self::$validate;
}
You can use Auth::login() method
static function register()
{
if(self::$validate['message'])
{
$user = User::create([
'name' => self::$values['name'],
'email' => self::$values['email'],
'password' => Hash::make(self::$values['password'])
]);
Auth::login($user);
return result::repsonse(true);
} else
return self::$validate;
}
My form validation is not working in Laravel. How can I update my form with validation in Laravel?
You can check my code here-
public function update(Request $request, $id)
{
$id->validate([
'Name'=>'required',
'UserName'=>'required',
'Password'=>'required|min:6',
'email'=>'required|email',
]);
$updateInfo= Info::findOrFail($id);
$updateInfo->user_id = $request->input('user_id');
$updateInfo->Name = $request->input('Name');
$updateInfo->UserName = $request->input('UserName');
$updateInfo->Password = $request->input('Password');
$updateInfo->save();
return redirect('/info');
}
You need to call validate on $request, like this-
$request->validate([
'Name'=>'required',
'UserName'=>'required',
'Password'=>'required|min:6',
'email'=>'required|email',
]);
Here is the full code-
public function update(Request $request, $id)
{
$request->validate([
'Name'=>'required',
'UserName'=>'required',
'Password'=>'required|min:6',
'email'=>'required|email',
]);
if (!$validator->fails()) {
$updateInfo= Info::findOrFail($id);
$updateInfo->user_id = $request->input('user_id');
$updateInfo->Name = $request->input('Name');
$updateInfo->UserName = $request->input('UserName');
$updateInfo->Password = $request->input('Password');
$updateInfo->save();
} else {
\Session::flash('error', $validator->messages()->first());
return redirect()->back()->withInput();
}
return redirect('/info');
}
I have added one more condition in the code to handle the validation errors. If validation fails then it will redirect back with your inputs as well as the validation error messages. Make sure you have error session flash in your blade views to show the errors.
For me this is best way , i can keep on track on query and other exceptions by putting it in try catch block
public function update(Request $request, $id)
{
try{
$validator = Validator::make($request->all(), [
'name' => 'required',
'UserName' => 'required',
'Password' => 'required',
'email' => 'required|email',
]);
if($validator->fails()) {
return redirect()
->route('path_to_edit_form')
->withErrors($validator)
->withInput();
}
Info::where('id',$id)->update([
'user_id' => $request->get('user_id'),
'Name' => $request->get('Name'),
'UserName' => $request->get('UserName'),
'Password' => $request->get('Password'),
]);
return back()->with([
'alert_type' => 'success',
'message' => 'User info updated successfully.'
]);
}catch(\Exception $e){
return back()->with([
'alert_type' => 'danger',
'message' => $e->getMessage()
]);
}
}
I try to register user with send connfirmation mail at the samme time.but the page is reloaded there is no data stored in db and also not sending the confirmation email.
RegisterController.php
protected function create(array $data)
{
return User::create([
'title' => $data['title'],
'fname' => $data['fname'],
'lname' => $data['lname'],
'company' => $data['company'],
'email' => $data['email'],
'phone' => $data['phone'],
'people' => $data['people'],
'sdate' => $data['sdate'],
'notes' => $data['note'],
'password' => Hash::make($data['password']),
]);
}
public function register(Request $request)
{
//dd($request->all());
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
Mail::to($user->email)->send(new ConfirmationEmail($user));
return back()->with('status','Please confirm your email address...');
// $this->guard()->login($user);
// return $this->registered($request, $user)
// ?: redirect($this->redirectPath());
}
//confirmation email code
public function confirmEmail($token)
{
User::whereToken($token)->firstOrFail()->confirmEmail();
return redirect('/user/login')->with('status', 'You are now confirmed. Please login.');
}
When I am trying to register a user from mobile, that data is inserting in the users table but not in oauth_clients. It should upload at the same time.
When I try to login, it shows oauth/token 401 error
Here are my routes:
Route::post('/register' , 'ProviderAuth\TokenController#register');
Route::post('/oauth/token' , 'ProviderAuth\TokenController#authenticate');
This is my register function, which is in TokenConrtoller:
public function register(Request $request)
{
$this->validate($request, [
'device_id' => 'required',
'device_type' => 'required|in:android,ios',
'device_token' => 'required',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required|email|max:255|unique:providers',
'mobile' => 'required',
'password' => 'required|min:6|confirmed',
]);
try{
$Provider = $request->all();
$Provider['password'] = bcrypt($request->password);
$Provider['status'] = 'approved';
$Provider = Provider::create($Provider);
ProviderDevice::create([
'provider_id' => $Provider->id,
'udid' => $request->device_id,
'token' => $request->device_token,
'type' => $request->device_type,
]);
return $Provider;
} catch (QueryException $e) {
if ($request->ajax() || $request->wantsJson()) {
return response()->json(['error' => 'Something went wrong, Please try again later!'], 500);
}
return abort(500);
}
}
This is my authenticate method:
public function authenticate(Request $request)
{
$this->validate($request, [
'device_id' => 'required',
'device_type' => 'required|in:android,ios',
'device_token' => 'required',
'email' => 'required|email',
'password' => 'required|min:6',
]);
Config::set('auth.providers.users.model', 'App\Provider');
$credentials = $request->only('email', 'password');
try {
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'The email address or password you entered is incorrect.'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'Something went wrong, Please try again later!'], 500);
}
$User = Provider::with('service', 'device')->find(Auth::user()->id);
$User->access_token = $token;
$User->currency = Setting::get('currency', '$');
if($User->device) {
if($User->device->token != $request->token) {
$User->device->update([
'udid' => $request->device_id,
'token' => $request->device_token,
'type' => $request->device_type,
]);
}
} else {
ProviderDevice::create([
'provider_id' => $User->id,
'udid' => $request->device_id,
'token' => $request->device_token,
'type' => $request->device_type,
]);
}
return response()->json($User);
}
I think you should register in JWT auth Like
$token = JWTAuth::fromUser($user);
after Register User
Check this Link for more information
https://blog.pusher.com/laravel-jwt/
I'm using laravel with JWT Auth to connect my laravel project to mobile, this is my api controller at laravel
public function login(Request $request) {
$credentials = $request->only('email', '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
$user = Sentry::authenticate($credentials, false);
return response()->json([
'code' => '200',
'message' => 'success',
'last_updated' => $user->updated_at->format("Y-m-d\TH:i:s\Z"),
'data' => [
'id' => $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
'username' => $user->username,
'token' => $token
]
]);
}
but how to set the credentials using email or username with this JWT?
If you are using "tymon/jwt-auth" package, then you can just pass the user object to the JWTAuth class and bam!, you can get the JWT token in return which you can use to make user go through the app.
$user = User::where('email', $email)
->orWhere('username', $username)
->first();
$token = null;
if (!$token = JWTAuth::fromUser($get_info)) {
return $this->respondInternalError( 'Can\'t generate the JWT token right now, try again later!', 'object', 400);
}
return response()->json([
'code' => '200',
'message' => 'success',
'last_updated' => $user->updated_at->format("Y-m-d\TH:i:s\Z"),
'data' => [
'id' => $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
'username' => $user->username,
'token' => $token
]
]);
You can login using email or using the following format
public function login(Request $request)
{
//validate incoming request
$this->validate($request, [
'email_phone' => 'required|string',
'password' => 'required|string',
]);
try {
$login_type = filter_var( $request->email_phone, FILTER_VALIDATE_EMAIL ) ? 'email' : 'phone';
// return $login_type;
$credentials = [$login_type => $request->email_phone, 'password'=>$request->password];
if (! $token = Auth::attempt($credentials)) {
return response()->json($this->customResponse("failed", "Unauthorized"), 401);
}
return $this->respondWithToken($token);
} catch(JWTException $e) {
return response()->json($this->customResponse("failed", "An error occured, please contact support.", $user), 500);
}
}