Skip to content

Commit 00f3ef0

Browse files
committed
Added Facebook Authentication
1 parent 0cfd168 commit 00f3ef0

File tree

4 files changed

+132
-7
lines changed

4 files changed

+132
-7
lines changed

app/Http/Controllers/Api/AuthController.php

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public function register(Request $request) {
7272
'phone_number' => $phone_number,
7373
'password' => Hash::make( $input['password']),
7474
'confirmation_code' => $confirmation_code,
75-
'verification_code' => $verification_code
75+
'verification_code' => $verification_code,
76+
'type' => "email",
7677
]);
7778

7879
if($input['v_type'] === "sms"){
@@ -90,6 +91,99 @@ function($m) use ($email, $name){
9091
}
9192
}
9293

94+
/**
95+
* API Register User with FACEBOOK
96+
*
97+
* @param Request $request
98+
*/
99+
public function registerWithFacebook(Request $request) {
100+
$input = $request->only(
101+
'name',
102+
'email',
103+
'fbID'
104+
);
105+
106+
if(empty($input['email'])){
107+
return response()->json(['success'=> false, 'error'=> "No email address provided"]);
108+
}else{
109+
$user =
110+
User::where('email', '=', $input['email'])
111+
->where('type', "fb")
112+
->where('fbID', $input['fbID'])
113+
->first();
114+
115+
//Refactor the code below
116+
if ($user){
117+
//Log user in
118+
$email = $user->email;
119+
$password = $email;
120+
121+
$credentials = [
122+
'email' => $email,
123+
'password' => $password
124+
];
125+
126+
try {
127+
// attempt to verify the credentials and create a token for the user
128+
if (! $token = JWTAuth::attempt($credentials)) {
129+
return response()->json(['success' => false, 'error' => 'Invalid Credentials. Please make sure you entered the right information and you have verified your account. '], 401);
130+
}
131+
} catch (JWTException $e) {
132+
// something went wrong whilst attempting to encode the token
133+
return response()->json(['success' => false, 'error' => 'could_not_create_token'], 500);
134+
}
135+
136+
// all good so return the token
137+
return response()->json(compact('token'));
138+
139+
}else{
140+
echo "no user";
141+
//Register user
142+
$rules = [
143+
'name' => 'required|max:255',
144+
'email' => 'required|email|max:255|unique:users'
145+
];
146+
147+
$validator = Validator::make($input, $rules);
148+
//print_r($validator) ;
149+
150+
if($validator->fails()) {
151+
$error = $validator->messages()->toJson();
152+
return response()->json(['success'=> false, 'error'=> $error]);
153+
}
154+
155+
$email = $input['email'];
156+
$password = $email;
157+
User::create([
158+
'name' => $input['name'],
159+
'email' => $input['email'],
160+
'password' => Hash::make($password),
161+
'confirmed' => 1,
162+
'type' => "fb",
163+
'fbID' => $input['fbID']
164+
]);
165+
166+
$credentials = [
167+
'email' => $email,
168+
'password' => $password
169+
];
170+
171+
try {
172+
// attempt to verify the credentials and create a token for the user
173+
if (! $token = JWTAuth::attempt($credentials)) {
174+
return response()->json(['success' => false, 'error' => 'Invalid Credentials. Please make sure you entered the right information and you have verified your account. '], 401);
175+
}
176+
} catch (JWTException $e) {
177+
// something went wrong whilst attempting to encode the token
178+
return response()->json(['success' => false, 'error' => 'could_not_create_token'], 500);
179+
}
180+
181+
// all good so return the token
182+
return response()->json(compact('token'));
183+
}
184+
}
185+
}
186+
93187
/**
94188
* API Verify User
95189
*
@@ -183,8 +277,6 @@ public function recoverPassword(Request $request)
183277
*/
184278
public function login(Request $request)
185279
{
186-
// $credentials = $request->only('email', 'password');
187-
188280
$credentials = [
189281
'email' => $request->email,
190282
'password' => $request->password,
@@ -205,6 +297,36 @@ public function login(Request $request)
205297
return response()->json(compact('token'));
206298
}
207299

300+
/**
301+
* API Login with Facebook
302+
*
303+
* @param Request $request
304+
* @return \Illuminate\Http\JsonResponse
305+
*/
306+
public function loginWithFacebook($email, $password)
307+
{
308+
echo $email;
309+
echo $password;
310+
311+
$credentials = [
312+
'email' => $email,
313+
'password' => $password
314+
];
315+
316+
try {
317+
// attempt to verify the credentials and create a token for the user
318+
if (! $token = JWTAuth::attempt($credentials)) {
319+
return response()->json(['success' => false, 'error' => 'Invalid Credentials. Please make sure you entered the right information and you have verified your account. '], 401);
320+
}
321+
} catch (JWTException $e) {
322+
// something went wrong whilst attempting to encode the token
323+
return response()->json(['success' => false, 'error' => 'could_not_create_token'], 500);
324+
}
325+
326+
// all good so return the token
327+
return response()->json(compact('token'));
328+
}
329+
208330
/**
209331
* Log out
210332
* Invalidate the token, so user cannot use it anymore

app/Http/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//anything goes here will be under api/
2222
Route::post('login', 'Api\AuthController@login');
2323
Route::post('register', 'Api\AuthController@register');
24+
Route::post('registerwithfb', 'Api\AuthController@registerWithFacebook');
2425
Route::get('verify/{type}/{confirmationCode}', 'Api\AuthController@verifyUser');
2526
Route::post('resend', 'Api\AuthController@resendVerification');
2627
Route::post('recover', 'Api\AuthController@recoverPassword');

app/User.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ class User extends Authenticatable
1212
* @var array
1313
*/
1414
protected $fillable = [
15-
'name', 'email', 'password', 'confirmation_code', 'verification_code'
16-
];
17-
15+
'name', 'email', 'password', 'confirmation_code', 'verification_code', 'confirmed', 'type', 'fbID'
16+
];
1817
/**
1918
* The attributes that should be hidden for arrays.
2019
*

database/migrations/2014_10_12_000000_create_users_table.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ public function up()
1616
$table->increments('id');
1717
$table->string('name');
1818
$table->string('email')->unique();
19-
$table->number('phone_number')->unique();
19+
$table->string('phone_number')->unique();
2020
$table->string('password');
2121
$table->boolean('confirmed')->default(0);
2222
$table->string('confirmation_code')->nullable();
2323
$table->string('verification_code')->nullable();
24+
$table->string('type')->nullable();
25+
$table->string('fbID')->nullable();
2426
$table->rememberToken();
2527
$table->timestamps();
28+
2629
});
2730
}
2831

0 commit comments

Comments
 (0)