Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppHttpControllers;
- use IlluminateHttpRequest;
- use AppHttpRequests;
- use AppUser;
- use JWTAuth;
- use AppHttpControllersController;
- use TymonJWTAuthExceptionsJWTException;
- class AuthenticateController extends Controller{
- public function __construct(){
- $this->middleware('jwt.auth',['except'=>['authenticate']]);
- }
- public function index(){
- return "Auth index";
- }
- public function authenticate(Request $request){
- $credentials =$request->only('phonenumber','generatednexmotoken');
- try{
- //verify the credentials and create a token for the user_error
- if($token=JWTAuth::attempt($credentials)){
- return response()->json(['error'=>'invalid_credentials']);
- }else{
- return response()->json(compact('token'));
- }
- }catch(TymonJWTAuthExceptionsJWTException $e){
- return response()->json(['error'=>'could not create toke']);
- }
- }
- public function getAuthenticatedUser(){
- try{
- if(! $user = JWTAuth::parseToken()->authenticate()){
- return responnse()->json(['user not found',404]);
- }else{
- return response()->json(compact('user'));
- }
- }catch(TymonJWTAuthExceptionsTokenExpiredException $e){
- return response()->json(['token expired'],$e->getStatusCode());
- }catch(TymonJWTAuthExceptionsTokenInvalidExceptions $e){
- return response()->json(['invalid token'],$e->getStatusCode());
- }catch(TymonJWTAuthExceptionsJWTException $e){
- return response()->json(['token is absent lol'],$e-
- >getStatusCode());
- }
- }
- }
- <?php
- namespace App;
- use IlluminateNotificationsNotifiable;
- use IlluminateFoundationAuthUser as Authenticatable;
- class User extends Authenticatable
- {
- use Notifiable;
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $fillable = [
- 'phonenumber', 'generatednexmotoken',
- ];
- /**
- * The attributes that should be hidden for arrays.
- *
- * @var array
- */
- protected $hidden = [
- 'generatednexmotoken', 'jwttoken',
- ];
- }
- Route::group(['prefix' => 'v1'], function()
- {
- Route::post('authenticate', 'AuthenticateController@authenticate');
- Route::get('authenticate/user',
- 'AuthenticateController@getAuthenticatedUser');
- });
- Route::get('/sms/send/{to}', function(NexmoClient $nexmo, $to){
- $message = $nexmo->message()->send([
- 'to' => $to,
- 'from' => '@leggetter',
- 'text' => 'Sending SMS from Laravel. Woohoo!'
- ]);
- Log::info('sent message: ' . $message['message-id']);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement