Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppHttpControllers;
  4.  
  5. use IlluminateHttpRequest;
  6. use AppHttpRequests;
  7. use AppUser;
  8. use JWTAuth;
  9. use AppHttpControllersController;
  10. use TymonJWTAuthExceptionsJWTException;
  11.  
  12. class AuthenticateController extends Controller{
  13.  
  14. public function __construct(){
  15. $this->middleware('jwt.auth',['except'=>['authenticate']]);
  16. }
  17.  
  18. public function index(){
  19. return "Auth index";
  20. }
  21.  
  22. public function authenticate(Request $request){
  23. $credentials =$request->only('phonenumber','generatednexmotoken');
  24. try{
  25. //verify the credentials and create a token for the user_error
  26. if($token=JWTAuth::attempt($credentials)){
  27. return response()->json(['error'=>'invalid_credentials']);
  28. }else{
  29. return response()->json(compact('token'));
  30. }
  31. }catch(TymonJWTAuthExceptionsJWTException $e){
  32. return response()->json(['error'=>'could not create toke']);
  33. }
  34. }
  35.  
  36. public function getAuthenticatedUser(){
  37. try{
  38. if(! $user = JWTAuth::parseToken()->authenticate()){
  39. return responnse()->json(['user not found',404]);
  40. }else{
  41. return response()->json(compact('user'));
  42. }
  43. }catch(TymonJWTAuthExceptionsTokenExpiredException $e){
  44. return response()->json(['token expired'],$e->getStatusCode());
  45. }catch(TymonJWTAuthExceptionsTokenInvalidExceptions $e){
  46. return response()->json(['invalid token'],$e->getStatusCode());
  47. }catch(TymonJWTAuthExceptionsJWTException $e){
  48. return response()->json(['token is absent lol'],$e-
  49. >getStatusCode());
  50. }
  51. }
  52. }
  53.  
  54. <?php
  55.  
  56. namespace App;
  57.  
  58. use IlluminateNotificationsNotifiable;
  59. use IlluminateFoundationAuthUser as Authenticatable;
  60.  
  61. class User extends Authenticatable
  62. {
  63. use Notifiable;
  64.  
  65. /**
  66. * The attributes that are mass assignable.
  67. *
  68. * @var array
  69. */
  70. protected $fillable = [
  71. 'phonenumber', 'generatednexmotoken',
  72. ];
  73.  
  74. /**
  75. * The attributes that should be hidden for arrays.
  76. *
  77. * @var array
  78. */
  79. protected $hidden = [
  80. 'generatednexmotoken', 'jwttoken',
  81. ];
  82. }
  83.  
  84. Route::group(['prefix' => 'v1'], function()
  85. {
  86. Route::post('authenticate', 'AuthenticateController@authenticate');
  87. Route::get('authenticate/user',
  88. 'AuthenticateController@getAuthenticatedUser');
  89. });
  90.  
  91. Route::get('/sms/send/{to}', function(NexmoClient $nexmo, $to){
  92. $message = $nexmo->message()->send([
  93. 'to' => $to,
  94. 'from' => '@leggetter',
  95. 'text' => 'Sending SMS from Laravel. Woohoo!'
  96. ]);
  97. Log::info('sent message: ' . $message['message-id']);
  98. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement