]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/AuthController.php
f290aeabbea5bd914dcb01bbda866b59303da96d
[bookstack] / app / Http / Controllers / Auth / AuthController.php
1 <?php
2
3 namespace Oxbow\Http\Controllers\Auth;
4
5 use Oxbow\Exceptions\SocialSignInException;
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', ['only' => ['getLogin', 'postLogin', 'getRegister']]);
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 registration form.
76      *
77      * @return \Illuminate\Http\Response
78      */
79     public function getRegister()
80     {
81         $socialDrivers = $this->socialAuthService->getActiveDrivers();
82         return view('auth.register', ['socialDrivers' => $socialDrivers]);
83     }
84
85     /**
86      * Show the application login form.
87      *
88      * @return \Illuminate\Http\Response
89      */
90     public function getLogin()
91     {
92
93         if (view()->exists('auth.authenticate')) {
94             return view('auth.authenticate');
95         }
96
97         $socialDrivers = $this->socialAuthService->getActiveDrivers();
98         return view('auth.login', ['socialDrivers' => $socialDrivers]);
99     }
100
101     /**
102      * Redirect to the relevant social site.
103      * @param $socialDriver
104      * @return \Symfony\Component\HttpFoundation\RedirectResponse
105      */
106     public function getSocialLogin($socialDriver)
107     {
108         return $this->socialAuthService->startLogIn($socialDriver);
109     }
110
111     /**
112      * The callback for social login services.
113      *
114      * @param $socialDriver
115      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
116      * @throws SocialSignInException
117      */
118     public function socialCallback($socialDriver)
119     {
120         return $this->socialAuthService->handleCallback($socialDriver);
121     }
122
123     /**
124      * Detach a social account from a user.
125      * @param $socialDriver
126      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
127      */
128     public function detachSocialAccount($socialDriver)
129     {
130         return $this->socialAuthService->detachSocialAccount($socialDriver);
131     }
132
133 }