z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.15k stars 2.82k forks source link

Socialite with Laravel admin #3348

Closed pcfjojo closed 5 years ago

pcfjojo commented 5 years ago

Laravel Default Auth instead of using laravel-admin default auth.

Is it possible to use Laravel authentication instead of default admin auth.?

*Working with laravel socialite, for the site which already has laravel-admin as the default authentication. I face difficulties to implement this any suggestion please?

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Socialite;
use App\User;
use Auth;

class SocialController extends Controller
{
    public function redirect($provider)
    {
         return Socialite::driver($provider)->redirect();
    }

    public function Callback($provider){
        $userSocial = Socialite::driver($provider)->stateless()->user();
        $user = User::where(['email' => $userSocial->getEmail()])->first();
        if($user){
           // Auth::login($user);
            // return redirect('/profile');
            if ($this->guard()->login($user)) {
                return $this->sendLoginResponse($request);
            }

            return back()->withInput()->withErrors([
                $this->username() => $this->getFailedLoginMessage(),
            ]);

        }else{
            $user = User::create([
                'name'          => $userSocial->getName(),
                'username'      => $userSocial->getEmail(),
                'email'         => $userSocial->getEmail(),
                'image'         => $userSocial->getAvatar(),
                'provider_id'   => $userSocial->getId(),
                'provider'      => $provider,
                'provider'      => $user->token,
            ]);
        //  return redirect()->route('/');
            if ($this->guard()->login($user)) {
                return $this->sendLoginResponse($request);
            }

            return back()->withInput()->withErrors([
                $this->username() => $this->getFailedLoginMessage(),
            ]);
        }
    } 

    protected function guard()
    {
        return Auth::guard('admin');
    }

    protected function username()
    {
        return 'username';
    }

    protected function sendLoginResponse(Request $request)
    {
        admin_toastr(trans('admin.login_successful'));

        $request->session()->regenerate();

        return redirect()->intended($this->redirectPath());
    }

    protected function redirectPath()
    {
        if (method_exists($this, 'redirectTo')) {
            return $this->redirectTo();
        }

        return property_exists($this, 'redirectTo') ? $this->redirectTo : config('admin.route.prefix');
    }

    protected function getFailedLoginMessage()
    {
        return \Lang::has('auth.failed')
            ? trans('auth.failed')
            : 'These credentials do not match our records.';
    }
}
I already use users table for the admin authentication and i have CustomerUserAuthentication .
stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.