dcblogdev / laravel-microsoft-graph

Laravel package for Microsoft Graph API (Microsoft365)
https://dcblog.dev/docs/laravel-microsoft-graph
Other
122 stars 52 forks source link

User is not being added to database #26

Closed Devannn closed 2 years ago

Devannn commented 2 years ago

Hello, when i try to log in with microsoft everything works fine and i get redirected to my MSGRAPH_LANDING_URL= but the account is not being added to the database.

dcblogdev commented 2 years ago

I've run through this and it's working on a new install, the user_id will be blank but I can see the email and token

innovaweb-dev commented 2 years ago

@Devannn, do you speak about the token or user informations which are not added in database ?

Also I your recommand to look at this video in order check your app registration is correct on Azure AD though Postman. https://www.youtube.com/watch?v=hWP-8QhiTeM If Postman retrun the token from Azure AD, your a ready to use dcblogdev/laravel-microsoft-graph

dcblogdev commented 2 years ago

The user_is is empty when login in with graph but the email and tokens will be set, you can see this in this video. https://www.youtube.com/watch?v=fvgd2RYc3G0

innovaweb-dev commented 2 years ago

I just look at for your video @dcblogdev it is very appreciated 👍

However, I think that I have the same problem of @Devannn. The access_token adds well on ms_graph_token table, but the users table keep empty. Also when a new user logs, the previous token is replaced by the new token although if user is different.

So, if you look at NewMicrosoft365SignInListener, normally the user should be add in users table. At what moment NewMicrosoft365SignInListener is trigger ?


I put this video to expose the problem. https://user-images.githubusercontent.com/2601347/183756367-eaacf1be-d8e5-4e0f-bd5e-f280acd7bad9.mp4

Thanks,

innovaweb-dev commented 2 years ago

@Devannn @dcblogdev I have found the problem, the NewMicrosoft365SignInListener and NewMicrosoft365SignInEvent are not registred so not triggerd. Laravel 9.23

// app\Providers\EventServiceProvider 
<?php

namespace App\Providers\EventServiceProvider;

use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

use Dcblogdev\MsGraph\Events\NewMicrosoft365SignInEvent;
use App\Listeners\NewMicrosoft365SignInListener;

class EventServiceProvider extends ServiceProvider
{

    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];

    public function boot()
    {
        Event::listen(
            NewMicrosoft365SignInEvent::class,
            [NewMicrosoft365SignInListener::class, 'handle']
        );
    }

    public function shouldDiscoverEvents()
    {
        return false;
    }
}
dcblogdev commented 2 years ago

ahh I see what you mean, thanks for helping to clear this up

innovaweb-dev commented 2 years ago

I think there are other problems in the code.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'test@mydomain.fr' for key 'users_email_unique'

if ($token->user_id == null) {
    $user = User::create(['name'     => $event->token['info']['displayName'],
        'email'    => $event->token['info']['mail'],
         'password' => '']);

This Is normal because $token->user_id always null, but if the user did connected in the past he is aleary exist on users table with a identique email at the email of $token.


Do you want to I create a new issue or we continue here ?

dcblogdev commented 2 years ago

when the event listerner is added to the event service provider that appears to solve the problem. If not please open a new issue.