]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/AuthController.php
Refactored Social auth into service, Made entity an abstract class
[bookstack] / app / Http / Controllers / Auth / AuthController.php
1 <?php
2
3 namespace Oxbow\Http\Controllers\Auth;
4
5 use Oxbow\Exceptions\UserNotFound;
6 use Oxbow\Services\SocialAuthService;
7 use Oxbow\User;
8 use Validator;
9 use Oxbow\Http\Controllers\Controller;
10 use Illuminate\Foundation\Auth\ThrottlesLogins;
11 use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
12
13 class AuthController extends Controller
14 {
15     /*
16     |--------------------------------------------------------------------------
17     | Registration & Login Controller
18     |--------------------------------------------------------------------------
19     |
20     | This controller handles the registration of new users, as well as the
21     | authentication of existing users. By default, this controller uses
22     | a simple trait to add these behaviors. Why don't you explore it?
23     |
24     */
25
26     use AuthenticatesAndRegistersUsers, ThrottlesLogins;
27
28     protected $loginPath = '/login';
29     protected $redirectPath = '/';
30     protected $redirectAfterLogout = '/login';
31
32     protected $socialAuthService;
33
34     /**
35      * Create a new authentication controller instance.
36      * @param SocialAuthService $socialAuthService
37      */
38     public function __construct(SocialAuthService $socialAuthService)
39     {
40         $this->middleware('guest', ['except' => 'getLogout']);
41         $this->socialAuthService = $socialAuthService;
42     }
43
44     /**
45      * Get a validator for an incoming registration request.
46      *
47      * @param  array $data
48      * @return \Illuminate\Contracts\Validation\Validator
49      */
50     protected function validator(array $data)
51     {
52         return Validator::make($data, [
53             'name'     => 'required|max:255',
54             'email'    => 'required|email|max:255|unique:users',
55             'password' => 'required|confirmed|min:6',
56         ]);
57     }
58
59     /**
60      * Create a new user instance after a valid registration.
61      *
62      * @param  array $data
63      * @return User
64      */
65     protected function create(array $data)
66     {
67         return User::create([
68             'name'     => $data['name'],
69             'email'    => $data['email'],
70             'password' => bcrypt($data['password']),
71         ]);
72     }
73
74     /**
75      * Show the application login form.
76      *
77      * @return \Illuminate\Http\Response
78      */
79     public function getLogin()
80     {
81
82         if (view()->exists('auth.authenticate')) {
83             return view('auth.authenticate');
84         }
85
86         $socialDrivers = $this->socialAuthService->getActiveDrivers();
87
88         return view('auth.login', ['socialDrivers' => $socialDrivers]);
89     }
90
91     /**
92      * Redirect to the relevant social site.
93      * @param $socialDriver
94      * @return \Symfony\Component\HttpFoundation\RedirectResponse
95      */
96     public function getSocialLogin($socialDriver)
97     {
98         return $this->socialAuthService->logIn($socialDriver);
99     }
100
101     /**
102      * The callback for social login services.
103      *
104      * @param $socialDriver
105      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
106      * @throws UserNotFound
107      */
108     public function socialCallback($socialDriver)
109     {
110         $user = $this->socialAuthService->getUserFromCallback($socialDriver);
111         \Auth::login($user, true);
112         return redirect($this->redirectPath);
113     }
114
115 }