Created
October 20, 2017 17:34
-
-
Save aynm142/d536a5138403c09dee483cdc729019e2 to your computer and use it in GitHub Desktop.
api.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @SWG\Post( | |
* path="/login/api", | |
* summary="Store new or update existing device", | |
* tags={"Login"}, | |
* description="Store new or update existing device. <strong>Authorization header required</strong>", | |
* operationId="postDevice", | |
* consumes={"application/json", "application/x-www-form-urlencoded"}, | |
* produces={"application/json"}, | |
* @SWG\Parameter( | |
* name="login", | |
* in="query", | |
* description="login", | |
* required=true, | |
* type="string" | |
* ), | |
* @SWG\Parameter( | |
* name="password", | |
* in="query", | |
* description="password", | |
* required=true, | |
* type="string" | |
* ), | |
* @SWG\Parameter( | |
* name="device_token", | |
* in="query", | |
* description="device_token", | |
* required=true, | |
* type="string" | |
* ), | |
* @SWG\Response( | |
* response=200, | |
* description="Successful operation, device created/updated" | |
* ) | |
* ) | |
* ) | |
*/ | |
//================= SWAGGER | |
/** | |
* Api for login on android device | |
* | |
* @param Request $request | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
public function api(Request $request) | |
{ | |
// validate request data | |
$validator = \Validator::make($request->all(), [ | |
'login' => 'required', | |
'password' => 'required', | |
'device_token' => 'required', | |
]); | |
// return bad request if validator fails | |
if ($validator->fails()) { | |
return response()->json(['response' => 'bad request']); | |
} | |
// check if login exists | |
$user = User::where('name', $request->login)->first(); | |
if ( !$user ) { | |
return response()->json(['response' => 'Login not found'], 401); | |
} | |
// check the password | |
if (!password_verify($request->password, $user->password)) { | |
return response()->json(['response' => 'Wrong password'], 401); | |
} | |
$user->device_token = $request->device_token; | |
try { | |
$user->save(); | |
} catch (QueryException $e) { | |
return response()->json(['response' => 'device token must be unique']); | |
} | |
return response()->json(['response' => 'OK!'], 200); | |
} | |
/** | |
* @SWG\Post( | |
* path="/api/register", | |
* summary="Store new or update existing device", | |
* tags={"Register"}, | |
* description="Register new user", | |
* operationId="postDevice", | |
* consumes={"application/json", "application/x-www-form-urlencoded"}, | |
* produces={"application/json"}, | |
* @SWG\Parameter( | |
* name="name", | |
* in="query", | |
* description="login", | |
* required=true, | |
* type="string" | |
* ), | |
* @SWG\Parameter( | |
* name="email", | |
* in="query", | |
* description="email", | |
* required=true, | |
* type="string" | |
* ), | |
* @SWG\Parameter( | |
* name="password", | |
* in="query", | |
* description="password", | |
* required=true, | |
* type="string" | |
* ), | |
* @SWG\Parameter( | |
* name="birthday", | |
* in="query", | |
* description="format dd/mm/YYYY", | |
* required=true, | |
* type="string" | |
* ), | |
* @SWG\Parameter( | |
* name="gender", | |
* in="query", | |
* description="gender male/famale", | |
* required=true, | |
* type="string" | |
* ), | |
* @SWG\Parameter( | |
* name="mobile", | |
* in="query", | |
* description="mobile", | |
* required=true, | |
* type="string" | |
* ), | |
* @SWG\Parameter( | |
* name="postcode", | |
* in="query", | |
* description="postcode", | |
* required=true, | |
* type="string" | |
* ), | |
* @SWG\Response( | |
* response=200, | |
* description="Successful operation, new user was created" | |
* ), | |
* ) | |
*/ | |
//================= SWAGGER | |
public function registerAPI(RegisterRequest $request) | |
{ | |
$birthday = $request->birthday; | |
try { | |
$birthday = Carbon::createFromFormat('d/m/Y', $request->get('birthday')); | |
} catch (\Exception $e) { | |
return $request->response(['birthday' => 'invalid format']); | |
} | |
$user = $request->all(); | |
$user['password'] = bcrypt($user['password']); | |
$user['device_token'] = str_random(); | |
$user['avatar_link'] = 'https://www.gravatar.com/avatar/'. md5($request->email) .'?d=retro'; | |
$user['birthday'] = $birthday; | |
User::create($user); | |
return response()->json(['response' => 'User was created successfully']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment