Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

strmizo's avatar

How to implement Sign up Via Facebook ?

Hello guys, I'm trying to implement Login / Signup via facebook, i'm using Socialite for Laravel 5, i'm not really getting this very clear in my head. Here is how i have implemented it in my app

1 - a button in login / sign up page, for login or sign up via facebook 2 - it has the same route, which then i test if i don't have code i redirect him to facebook then it's gets back with user info. 3. then i test if i have facebook id in my user table under "fb_id" column if so i log him by ID 4 - if facebook id is not in users table, i try to register him with random password and get the email and fb_id from facebook request !

The problem here, if a user want to JUST to login, if he logged in with a different facebook then he will ended up registering in the app! because that facebook account is not in the app !

so, i'm asking you guys, do i miss something or my implementation is not correct ! I really need your help

0 likes
4 replies
nolros's avatar

I've read your post 3 times and unsure what you asking. Multi-FB accounts? Or how to login not using FB post FB authentication?

1 like
strmizo's avatar

Excuse my english, i'm not a native english speaker. What i'm referring here in my post, is i want to let users sign up with there Facebook account into my account, like Pinterest does, and log them in the same way. The problem here, as you know that in Sign Up/Login process is the same, Socialite just get the authorisation from user to get access to their account and get Facebook ID, Email and some other info.

Sign Up

  • 1. Click on Authorise The App to get info from his account
  • 2. Socialite will get the user info Facebook ID and Email
  • 3. Save his info in Users table and generate a password
  • Login

  • 1. Click on authorise the app to get his info
  • 2. Socialite will get Facebook ID and Email
  • 3. Look for if these info matches in Database
  • 4. if so Log him in
  • The problem here how to know if the user tries to Sign up or Log in ! because there is just ONE REDIRECT URL, that you set in config of Socialite and in Facebook Application. I really hope i made my point !

    nolros's avatar
    nolros
    Best Answer
    Level 23

    @strmizo no problem, is there a reason you care if the user info is used for login or signup, I ask as it two parts of the same process. You signup to login. The decision to signup or login would be the user depending on whether they think they are a registered user or not. If they are not a member and choose sign in you would check based on the FB ID and email response and create them anyway. findOrCreate() is a great method for a case where you don't know, if the user exists then If a user signs up you are going to log them in anyway with a Welcome to MyApp. My point is either way you are signing them up and logging them in, or at least checking independent of their choice. You might have a flag in the User table for has_logged_in, if false then assume this is the first time and redirect them to welcome screen, once that click on the welcome screen "next" button you set the flag to true.

    Example, something like this in your User Repository, Model or a UserSocial table (which probably makes more sense).

        public function findOrCreateSocialUser($userId, $provider, $socialdata)
        {
    
            return UserSocial::firstOrCreate([
                'user_id'      => $userId,
                'provider'     => $provider,
                'token'        => $socialdata->token,
                'name'         => $socialdata->name,
                'avatar'       => $socialdata->avatar
            ]);
    
        }
    
    Shijinmon's avatar

    Hi Nolros, Your answer is very useful for me, but i dont get user email id from fb responce. how can i get email id.? iam using laravel/socialite 2. Please help me?

    Please or to participate in this conversation.