3

I am fairly new to stack overflow so please stick with me here.

I am creating a laravel project that has the following links...

main.dev (This is for login, registration, and help sections for the user)

athlete.main.dev (This is for athletes to be redirected to upon main.dev login)

coach.main.dev (This is for coaches to be redirect to upon main.dev login)

Here is an idea of what the code looks like to redirect the user switch (User::getGroup()) { case 'Athlete': return Redirect::to('http://athlete.main.dev'); case 'Coach': return Redirect::to('http://coach.main.dev'); }

This works fine and the user is redirected according to their account type, but upon arriving at this subdomain the user is currently forced to log in again.

I have been searching now for about three days trying to find the answer to no avail.

Is it possible to automatically login the user upon arriving at these redirects based off of main.dev's login information? Or how can I share the session data across subdomains?

Thanks for your help, -Nick

gofish
  • 347
  • 3
  • 10
  • http://athlete.main.dev this page is in your control I mean to say that you can code at here right ? – DsRaj Nov 05 '16 at 06:19
  • Subdomains are treated as different domains due to which your login isn't persisting. Check this http://stackoverflow.com/questions/3040514/are-ajax-calls-to-a-sub-domain-considered-cross-site-scripting – Shubhamoy Nov 05 '16 at 06:23
  • @DsRaj I do have control of the subdomain and use a middleware, domain, and namespace in the route group to handle it. – gofish Nov 05 '16 at 16:35

3 Answers3

2

I think you will require a SSO (Single Sign-on) feature to solve your problem as you need to serve both the applications on different server, it can only be achieved using SSO. Take a look at this it maybe helpful to solve your issue -

Good single sign-on solution for Laravel

Community
  • 1
  • 1
Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45
1

One way to solve you problem is by sending the username and password to the url you want to redirect and make an Auth::attemp() in the subdomain to.

switch (User::getGroup()) {
            case 'Athlete':
                return Redirect::to('http://athlete.main.dev')->with([
                'username' => $username,
                'password' => $password
                ]);
            case 'Coach':
                return Redirect::to('http://coach.main.dev')->with([
                'username' => $username,
                'password' => $password
                ]);;
       }

In your controller that handles coach.main.dev and athlete.main.dev add this before you make anything else

if(isset($username) && isset($password)){
    Auth::attempt(['username' => $username, 'password' => $password]);
}
Zenel Rrushi
  • 2,346
  • 1
  • 18
  • 34
0

Set this parameter in your .env file:

SESSION_DOMAIN='main.dev'

or hard code it in config/session.php

Valentino
  • 465
  • 6
  • 17