Twill: Google login

Created on 14 Jan 2019  路  3Comments  路  Source: area17/twill

Hello!
Is there any way to enable Google login? I found some mentions of Google login in the code but looks like it's not implemented yet.

enhancement question

Most helpful comment

Hi @newvladimirov!

You are right, we do have an option to enable Google Login but the actual implementation is not in the Twill codebase right now. This is something we've been implementing on some our projects that would require more documentation to be able to be made a turn-key solution for Twill users. However, we've provided the login button on our login screen, so that if you were to implement it in your project, you'd already have a nice design ready to be enabled by duplicating the login Blade view in your own Laravel project under resources/views/vendor/a17/twill/auth/login.blade.php. Then, you'd have to register a route and use it as the form action on the login view.

Here's an exemple controller action to do this thanks to Socialite:

public function loginWithGoogle(User $users, Socialite $socialite)
{
    if (!request()->has('code')) {
        return $socialite->driver('google')
            ->scopes(['profile', 'email'])
            ->redirect();
    }

    try {
        $googleUser = $socialite->driver('google')->user();
    } catch (\Exception $e) {
        flash()->message(trans('auth.failed'), FlashLevel::ERROR, false);
        return redirect()->route('auth.index');
    }

    // here you'd want to create or find an existing Twill user
    // $user = $users->where(['google_id' => $googleUser->id])
    //     ->orWhere(['email' => $googleUser->email])
    //     ->first();
    //
    // if (!$user) {
    //     $users->create([
    //         'google_id' => $googleUser->id,
    //         'name' => $googleUser->name,
    //         'email' => $googleUser->email,
    //         'role' => 'VIEWONLY',
    //         'published' => false,
    //     ]);
    // }

    Auth::login($user, true);

    return redirect()->intended(route('admin.dashboard'));
}

We recommend, when possible, that you only authorize users of a specific domain. To do that, use the following when redirecting the user to the Google login page:

if (!request()->has('code')) {
    return $socialite->driver('google')
        ->scopes(['profile', 'email'])
        ->with(['hd' => 'yourdomain.com'])
        ->redirect();
}

You could then add an additional check before really creating a user or authorizing the login:

$isStaff = isset($googleUser->user['domain']) && $googleUser->user['domain'] === 'yourdomain.com' && ends_with($googleUser->email, 'yourdomain.com');

This is the kind of rules we can't really determine for everyone's use case, but it would be great if we could provide an implementation that works for multiple use cases, that's for sure!

All 3 comments

Hi @newvladimirov!

You are right, we do have an option to enable Google Login but the actual implementation is not in the Twill codebase right now. This is something we've been implementing on some our projects that would require more documentation to be able to be made a turn-key solution for Twill users. However, we've provided the login button on our login screen, so that if you were to implement it in your project, you'd already have a nice design ready to be enabled by duplicating the login Blade view in your own Laravel project under resources/views/vendor/a17/twill/auth/login.blade.php. Then, you'd have to register a route and use it as the form action on the login view.

Here's an exemple controller action to do this thanks to Socialite:

public function loginWithGoogle(User $users, Socialite $socialite)
{
    if (!request()->has('code')) {
        return $socialite->driver('google')
            ->scopes(['profile', 'email'])
            ->redirect();
    }

    try {
        $googleUser = $socialite->driver('google')->user();
    } catch (\Exception $e) {
        flash()->message(trans('auth.failed'), FlashLevel::ERROR, false);
        return redirect()->route('auth.index');
    }

    // here you'd want to create or find an existing Twill user
    // $user = $users->where(['google_id' => $googleUser->id])
    //     ->orWhere(['email' => $googleUser->email])
    //     ->first();
    //
    // if (!$user) {
    //     $users->create([
    //         'google_id' => $googleUser->id,
    //         'name' => $googleUser->name,
    //         'email' => $googleUser->email,
    //         'role' => 'VIEWONLY',
    //         'published' => false,
    //     ]);
    // }

    Auth::login($user, true);

    return redirect()->intended(route('admin.dashboard'));
}

We recommend, when possible, that you only authorize users of a specific domain. To do that, use the following when redirecting the user to the Google login page:

if (!request()->has('code')) {
    return $socialite->driver('google')
        ->scopes(['profile', 'email'])
        ->with(['hd' => 'yourdomain.com'])
        ->redirect();
}

You could then add an additional check before really creating a user or authorizing the login:

$isStaff = isset($googleUser->user['domain']) && $googleUser->user['domain'] === 'yourdomain.com' && ends_with($googleUser->email, 'yourdomain.com');

This is the kind of rules we can't really determine for everyone's use case, but it would be great if we could provide an implementation that works for multiple use cases, that's for sure!

@ifox Great! Thanks a lot for the detailed answer!

Hey @ifox I have gotten google auth working with manual links in my redirects but I was hoping you could clarify 2 things on how to use your example.

Login controller redirects
With return redirect()->route('auth.index'); I get the error "Route [auth.index] not defined."

I am using the LoginController.php in the folder Auth

Web.php route replacement for the login page
I am using the folder structure for the admin page e.g. domain.com/admin and I can't override the login view at domain.com/admin/login'

If I create a new view it works.

Route::get('login', function () {
    return view('vendor.a17.twill.auth.login');
});

Any pointers are greatly appreciated 馃檹

Was this page helpful?
0 / 5 - 0 ratings

Related issues

karneaud picture karneaud  路  3Comments

sebastianlenton picture sebastianlenton  路  4Comments

jayhaluska picture jayhaluska  路  4Comments

stevanpavlovic picture stevanpavlovic  路  7Comments

madsem picture madsem  路  4Comments