authcontroller

Solutions on MaxInterview for authcontroller by the best coders in the world

showing results for - "authcontroller"
Maria José
13 Sep 2017
1namespace App\Http\Controllers;
2
3use Illuminate\Support\Facades\Auth;
4use Illuminate\Http\Request;
5use App\Models\User;
6use Illuminate\Support\Facades\Hash;
7
8class AuthController extends Controller
9{
10   //-----------------------------register users ---------------------------------
11   public function register(Request $request)
12   {
13       $password = password_hash($request->password, PASSWORD_DEFAULT);
14       $users_count= User::where('username',$request->username)->get();
15       $email_count= User::where('email',$request->email)->get();
16
17       $credentials = request(['username', 'password']);
18       dd(auth()->attempt($credentials));
19       
20       if(count($users_count)>0){
21           $data = [
22               'status' => 'username found',
23           ];
24       }
25       else {
26           if (count($email_count)>0){
27               $data = [
28                   'status' => 'email is found',
29               ];
30           }
31           else {
32               $user = User::create(
33                   [
34                       'username'=>$request->username,
35                       'password'=>$password,
36                       'email'=>$request->email,
37                       'fullname'=>$request->fullname,
38                       'natId'=>$request->nat_id,
39                       'role'=>'user'
40                   ]);
41
42               if($user){
43                   $credentials = request(['username', 'password']);
44
45                   if (! $token = auth()->attempt($credentials)) {
46                       return response()->json(['error' => 'Unauthorized'], 401);
47                   }
48                   else{
49                   $data = [
50                       'username'=>$request->username,
51                       'password'=>$password,
52                       'email'=>$request->email,
53                       'fullname'=>$request->fullname,
54                       'natId'=>$request->nat_id,
55                       'status' => 'success',
56                       'token' => $token
57                   ];}
58               }
59               else{
60                   $data = [
61                       'status' => 'failed',
62                   ];
63               } 
64           }
65       }
66       return $data;  
67   }
68} 
69
similar questions
queries leading to this page
authcontroller