Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. <?php
  2. namespace App\Repository;
  3.  
  4. use Auth0\Login\Contract\Auth0UserRepository as Auth0UserRepository;
  5. use App\User as User;
  6.  
  7. class UserRepository implements Auth0UserRepository {
  8.  
  9. public function getUserByDecodedJWT($jwt)
  10. {
  11. $jwt->user_id = $jwt->sub;
  12. return $this->upsertUser($jwt);
  13. }
  14.  
  15. public function getUserByUserInfo($userInfo)
  16. {
  17. return $this->upsertUser((object) $userInfo['profile']);
  18. }
  19.  
  20. /**
  21. * Check if user is in database, if not create.
  22. *
  23. * @return User
  24. */
  25. protected function upsertUser($profile) {
  26.  
  27. $user = User::where("auth0id", $profile->user_id)->first();
  28.  
  29. // create user if not in database
  30. if ($user === null) {
  31. $user = new User();
  32. $user->email = $profile->email;
  33. $user->auth0id = $profile->user_id;
  34. $user->name = $profile->name;
  35. // random password, we dont need it
  36. $user->password = md5(time());
  37. $user->save();
  38. }
  39.  
  40. return $user;
  41. }
  42.  
  43. public function getUserByIdentifier($identifier)
  44. {
  45.  
  46. //Get the user info of the user logged in (probably in session)
  47. $user = \App::make('auth0')->getUser();
  48. if ($user === null) return null;
  49.  
  50. // build the user
  51. $user = $this->getUserByUserInfo($user);
  52.  
  53. // it is not the same user as logged in, it is not valid
  54. if ($user && $user->id == $identifier) {
  55. return $user;
  56. }
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement