Guest User

Untitled

a guest
May 22nd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. class UploadController extends Controller {
  2.  
  3. public $serializer = [
  4. 'class' => 'yiirestSerializer',
  5. 'collectionEnvelope' => 'items',
  6. ];
  7.  
  8. public function behaviors()
  9. {
  10. $behaviors = parent::behaviors();
  11.  
  12. $behaviors['authenticator'] = [
  13. 'class' => JwtHttpBearerAuth::className()
  14. ];
  15.  
  16. return $behaviors;
  17. }
  18.  
  19. public function actionIndex() {
  20.  
  21. //Work with User
  22. }
  23. }
  24.  
  25. public function actionLogin()
  26. {
  27.  
  28. $username = Yii::$app->request->post('username');
  29. $password = Yii::$app->request->post('password');
  30.  
  31. $provider = new ActiveDataProvider([
  32. 'query' => User::find()
  33. ->where(['user_name' => $username])->asArray()->one(),
  34. ]);
  35.  
  36. $result = $provider->query;
  37.  
  38. if($result)
  39. {
  40. if (Yii::$app->getSecurity()->validatePassword($password, $result['user_pass']))
  41. {
  42. $tokenId = base64_encode(mcrypt_create_iv(32));
  43. $issuedAt = time();
  44. $notBefore = $issuedAt; //Adding 10 seconds
  45. $expire = $notBefore + 5184000; // Adding 60 Days
  46. $serverName = 'your-site.com';
  47. $data = [
  48. 'iat' => $issuedAt, // Issued at: time when the token was generated
  49. 'jti' => $tokenId, // Json Token Id: an unique identifier for the token
  50. 'iss' => $serverName, // Issuer
  51. 'nbf' => $notBefore, // Not before
  52. 'exp' => $expire, // Expire
  53. 'data' => [ // Data related to the signer user
  54. 'id' => $result['user_id'],
  55. 'username' => $result['user_name'],
  56. 'mobile' => $result['user_mobile'],
  57. 'email' => $result['user_email'],
  58. 'city' => $result['user_city'],
  59. 'state' => $result['user_state'],
  60. 'country' => $result['user_country'],
  61. 'picture' => $result['user_picture'],
  62. ]
  63. ];
  64.  
  65. $jwt = JWT::encode(
  66. $data,
  67. JWT_KEY,
  68. 'HS512'
  69. );
  70.  
  71. $response = [
  72. 'status' => true,
  73. 'message' => 'Login Success..',
  74. 'era_tkn' => $jwt,
  75. ];
  76. }
  77. else
  78. {
  79. $response = [
  80. 'status' => false,
  81. 'message' => 'Wrong username or password.',
  82. ];
  83. }
  84. }
  85. else
  86. {
  87. $response = [
  88. 'status' => false,
  89. 'message' => 'Wrong username or password.',
  90. ];
  91. }
  92.  
  93. return $response;
  94. }
  95.  
  96. public function check_token()
  97. {
  98. $headers = Yii::$app->request->headers;
  99. $token = $headers->get('era_tkn');
  100. if($token)
  101. {
  102. try{
  103. $valid_data = JWT::decode($token, JWT_KEY, array('HS512'));
  104. $valid_data = $valid_data->data;
  105.  
  106. }catch(Exception $e){
  107. $valid_data = $e->getMessage();
  108. }
  109. }
  110. else
  111. {
  112. $valid_data = 'Required Authentication';
  113. }
  114.  
  115. return $valid_data;
  116. }
  117.  
  118. $user_data = $this->check_token();
  119. if (!empty($user_data->id))
  120. {
  121. echo $user_data->id;
  122. }
  123. else
  124. {
  125. echo "Invalid Token.";
  126. }
Add Comment
Please, Sign In to add comment