Support the ongoing development of Laravel.io →
Socialite Authentication Laravel

In my Laravel-5.8 I am using Azure Socialite Login.

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = '/dashboard';
    protected $username = 'username';

    public function redirectToProvider()
    {
        return Socialite::with('azure')->redirect();
    }

    public function handleProviderCallback()
    {
         $azureUser = Socialite::with('azure')->user();
        try 
        {
            $user = User::where('email', $azureUser->email)->orWhere('username', $azureUser->user['mailNickname'])->first();
            if($user)
            {
                if(Auth::loginUsingId($user->id))
                {
                    $user->update([
                         'last_login_at' => now(),
                    ]);
                   return redirect()->intended('/dashboard');
                }
            }
        } 
        catch(\Exception $e) 
        {
            session()->flash("error", "Authentication failed, kindly contact the Administrator!");
            return redirect(route('login'));
        }
    }   
}


'azure' => [
   'client_id' => env('AZURE_KEY','mykey'),
   'client_secret' => env('AZURE_SECRET','myazuresecret'),
   'redirect' => env('AZURE_REDIRECT_URI','https://myapp/login/azure/callback')
],

This is working and it uses: https://myapp.com

But I'm now asked to make it multi-company application using single DB. There is a company table and each table have company_id

Each having this url:

https://myapp.com   
https://company1.myapp.com
https://company2.myapp.com

I now have these tables:

class Company extends Model
{
  protected $table = 'companies';
  protected $fillable = [
            'id',
            'company_name',
            'subdomain',
        ];
}

From the company table, where subdomain is null then it is https://myapp.com , while subdomain field could be company1 or company2 and so on.

class User extends Authenticatable
{
   protected $fillable = [
    'name', 
    'email', 
    'company_id',
    'password', 
 ];
}

The question is on the azure url redirect:

'redirect' => env('AZURE_REDIRECT_URI','https://myapp/login/azure/callback')

since it is redirecting to the main domain myapp.

What effect will this have on company1.myapp.com and company2.myapp?

How do I make it redirect to the subdomains also?

Thanks

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Michael noblemfd Joined 28 Mar 2019

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.