Welcome Guest, Not a member yet? Register   Sign In
Magic login link not working - SHIELD CI4
#1

I am using shield for Authorization, I have created API to send magic login link, email receives with magic link. when I click on link, it's makes the user login.
Earlier this whole process was working fine, but now suddenly stopped. 
I tried to test the default links as well, that too not working. Showing "Unable to verify the link." error
https://prnt.sc/Ws3I6O-RFjMA

Below is my code
PHP Code:
public function forgotPassword() {
        $currentLang $this->request->getVar("lang") ?? DEFAULT_LANGUAGE;
        $this->request->setLocale($currentLang);

        $rules = [
            "email"      => "required|valid_email"
        ];

        if(!$this->validate($rules)){
            $response = [
                "status"    => false,
                "message"  => $this->validator->getErrors(),
                "data"      => []
            ];
        } else {

            $userEmail $this->request->getVar('email');
            $users auth()->getProvider();
            
            $user 
$users->findByCredentials(['email' => $userEmail]);

            if($user){
                $firstName $user->first_name;

                // Generate a unique token
                $token bin2hex(random_bytes(16));

                // Store the token in the user's session or any temporary storage
                $session session();
                $session->set('magic_link_token'$token);

                // Generate the magic link URL
                $magicLinkUrl site_url("api/magic-login/{$token}");

                // sending email  here
                sendEmail($emailData);

                $response = [
                    "status"    => true,
                    //"message"  => 'Magic link sent on your email. Please login with this link and update your password.',
                    "message"  => lang('App.forgot_success'),
                    "data"      => ['magic_link_url' => $magicLinkUrl]
                ];
            } else {
                $response = [
                    "status"    => false,
                    "message"  => lang('App.email_notExist'),
                    "data"      => []
                ];
            }
            
        
}

        return $this->respondCreated($response);
    


Here is my code to verify link
PHP Code:
public function magicLogin($token)
    {
        $currentLang $this->request->getVar("lang") ?? DEFAULT_LANGUAGE;
        $this->request->setLocale($currentLang);
        
        
// Retrieve the token from the URL parameter
        // Validate the token, you may check it against the session or temporary storage
        $session session();
        $storedToken $session->get('magic_link_token');

        if ($token === $storedToken) {
            // Token is valid, log the user in
            $user auth()->user();

            $token $user->generateAccessToken(SECRET_KEY);
            $auth_token $token->raw_token;

            // Return a success message as a JSON response
            $response = [
                "status"    => true,
                "message"  => lang('App.login_success'),
                "data"      => ['token' => $auth_token]
            ];
        } else {
            // Invalid token
            $response = [
                "status"    => false,
                "message"  => lang('App.invalid_link'),
                "data"      => []
            ];
        

        return $this->respondCreated($response);
    

Please guide what is the issue. I searched alot regarding this, but didn't find anything regarding this.
Thanks
Reply
#2

Please Read:
CodeIgniter 4 Users Guide - IncomingRequest Class - getVar()
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thanks for the reply, I understand this link helpful to validate the incoming request, But I can't under how this resolve my issue.
Here is the old screenshot of my code execution, by using the magic link, I was able to authorise the user.
https://prnt.sc/fyba8ArKM_4I


I am not understanding how does code stopped working suddenly.
Thanks
Reply
#4

Shield is too complicated to implement when connected to a project for a beginner
Reply
#5

@pratibha.cts, I see nothing wrong with your code, I said the above because they say not to use getVar anymore.

Can you check and make sure that your email is working, google has been making email login changes that effect this.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(This post was last modified: 07-06-2024, 01:44 AM by luckmoshy.)

Google does not allow less security if they now use API which is the main reason it was from my side too
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#7

You can use your internet providers SMTP Mail server if you have internet connection.

I have Comcast and I use their SMTP Mail server for testing my emails.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

Hi

I checked the process today again on web and postman, there is no issue with email receiving. I received email on my gmail account.

https://prnt.sc/Ik00CovcsnYF
https://prnt.sc/iH4Lyjq9NWWR

getting this error in postman.
https://prnt.sc/kUGKs8AhHyov
Reply
#9

Are you using the newest version of postman, they had problems with some older versions.

Your Error is stating that the token is NULL.

Check your generateToken() method etc.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#10

(07-05-2024, 10:51 PM)InsiteFX Wrote: @pratibha.cts, I see nothing wrong with your code, I said the above because they say not to use getVar anymore.

Can you check and make sure that your email is working, google has been making email login changes that effect this.

(07-09-2024, 10:15 PM)InsiteFX Wrote: Are you using the newest version of postman, they had problems with some older versions.

Your Error is stating that the token is NULL.

Check your generateToken() method etc.



Yes.... I am using latest postman version, the issue is not only in postman, token in verifying on web page also
Here you can see token and stored token is same
https://prnt.sc/toanChkLcsQn


web screenshots
https://prnt.sc/pEhJ8oTHwmU7
https://prnt.sc/UGwyr5qub3lG
https://prnt.sc/klHrYl2sqXqD
https://prnt.sc/f6_0U_TwNmRH


Issue Screenshot
https://prnt.sc/sMEi79agGdz5

the line creating issue is working in other functions, but not working here

Please help.
Thanks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB