I'm building a laravel API. I want when i register , email verify will be sent activation code to user email automatically.
the problem is when i create a new activation code , i create a new record in tokens table too, this record has user_id
field , so for store it , i use JWTAuth::user()->id
but i have this error:
Trying to get property 'id' of non-object
i know why this happens , because I did not enter any tokens and i don't know how handle it and where to create a new record in tokens table
for more details I have :
AuthController
: Login and register
public function register(Request $request) {
$validator = Validator::make($request->all(), [
'name'=>'required|string|min:3|max:30',
'email' => 'required|string|email|max:100|unique:users',
'password' => 'required|string|confirmed|min:6',
]);
if($validator->fails()){
return response()->json($validator->errors()->toJson(), 400);
}
$user = User::create(array_merge(
$validator->validated(),
['password' => bcrypt($request->password)],
));
$token = JWTAuth::fromUser($user);
dd($this->sendNotification());
$user->$this->sendNotification();
return response()->json([
'message' => 'successfully created',
'user' => $user,
'token' => $token,
], 201);
}
EmailVerifyController
: for verify user email and validate activation code
public function emailVerify(Request $request){
$data = $request->validate([
'code' => 'required|size:10|numeric',
]);
$interedCode = (int)$data['code'];//convert code from string to integer
$userCode = Token::where('user_id' , JWTAuth::user()->id)->first();//find user from tokens table
$activationCode = $userCode->code; //get activation code of user in tokens table
$expires_in = (int)$userCode->expires_in; //get expire time of code
$now = Carbon::now()->timestamp;
if($interedCode == $activationCode) {
if ($now < $expires_in) {
$user = JWTAuth::user()->id;
$findUser = User::find($user);
$findUser->email_verified_at = Carbon::now()->timestamp;
$findUser->save();
$token = Token::where('user_id', JWTAuth::user()->id)->first();
$token->status = 1;
$token->save();
return response()->json('email verified successfully', 200);
} else {
return response()->json('code expired', 400);
}
}else{
return response()->json('wrong activation code' , 400);
}
}
SendNotificationTrait
: for send email and create a new record in token table
trait EmailVerifyTrait
{
public function sendNotification(){
$random = $this->generateVerificationCode(6);
$details = [
'title' => 'Mail from ItSolutionStuff.com',
'body' =>$random,
];
Mail::to('*****@gmail.com')->send(new VerifyMail($details));
return response()->json([
'message'=>'your email verification code sent to your email'
] , 201);
}
public function generateVerificationCode($length = 6) {
$characters = '0123456789';
$charactersLength = strlen($characters);
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $characters[rand(0, $charactersLength - 1)];
}
$token = new Token();
$token->user_id = JWTAuth::user()->id;
$token->code = $code;
$token->status = 0;
$token->save();
return $code;
}
tokens tables
: has this fields : user_id , code , created_at , expires_in
so how can i handle creating new Token record in tokens table ?
or should i use event listener ?
thank you for your help and sorry for my language.