Skip to content

Commit

Permalink
added docs on customizing the user management
Browse files Browse the repository at this point in the history
  • Loading branch information
glena committed Sep 15, 2016
1 parent 60b65aa commit 3bf1b17
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,67 @@ In the `register` method of your `AppServiceProvider` add:

You can implement your own cache strategy by creating a new class that implements the `Auth0\SDK\Helpers\Cache\CacheHandler` contract, or just use the cache strategy you want by picking that store with `Cache::store('your_store_name')`;

### Storing users in your database

You can customize the way you handle the users in your application by creating your own `UserRepository`. This class should implement the `Auth0\Login\Contract\Auth0UserRepository` contract.

```php
<?php
namespace App\Repository;

use Auth0\Login\Contract\Auth0UserRepository;

class MyCustomUserRepository implements Auth0UserRepository {

/* This class is used on api authN to fetch the user based on the jwt.*/
public function getUserByDecodedJWT($jwt) {
/*
* The `sub` claim in the token represents the subject of the token
* and it is always the `user_id`
*/
$jwt->user_id = $jwt->sub;

return $this->upsertUser($jwt);
}

public function getUserByUserInfo($userInfo) {
return $this->upsertUser($userInfo['profile']);
}

protected function upsertUser($profile) {

$user = User::where("auth0id", $profile->user_id)->first();

if ($user === null) {
// If not, create one
$user = new User();
$user->email = $profile->email; // you should ask for the email scope
$user->auth0id = $profile->user_id;
$user->name = $profile->name; // you should ask for the name scope
$user->save();
}

return $user;
}

public function getUserByIdentifier($identifier) {
//Get the user info of the user logged in (probably in session)
$user = \App::make('auth0')->getUser();

if ($user===null) return null;

// build the user
$user = $this->getUserByUserInfo($user);

// it is not the same user as logged in, it is not valid
if ($user && $user->auth0id == $identifier) {
return $auth0User;
}
}

}
```

###Laravel 5.2

####Routes
Expand Down
8 changes: 2 additions & 6 deletions src/Auth0/Login/Auth0UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public function __construct(Auth0UserRepository $userRepository, Auth0Service $a
* @return Authenticatable
*/
public function retrieveByID($identifier) {
dd('retrieveByID',$identifier);
return $this->userRepository->getUserByIdentifier($identifier);

}

public function retrieveByCredentials(array $credentials) {
Expand All @@ -57,22 +55,20 @@ public function retrieveByCredentials(array $credentials) {
* Required method by the UserProviderInterface, we don't implement it
*/
public function retrieveByToken($identifier, $token) {
dd('retrieveByToken',$identifier, $token);
return false;
}

/**
* Required method by the UserProviderInterface, we don't implement it
*/
public function updateRememberToken(Authenticatable $user, $token) {
dd('updateRememberToken',$user, $token);

}

/**
* Required method by the UserProviderInterface, we don't implement it
*/
public function validateCredentials(Authenticatable $user, array $credentials) {
dd('validateCredentials',$user, $credentials);
return false;
}
}
}
15 changes: 6 additions & 9 deletions src/Auth0/Login/Repository/Auth0UserRepository.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php namespace Auth0\Login\Repository;
<?php

namespace Auth0\Login\Repository;

use Auth0\Login\Auth0User;
use Auth0\Login\Auth0JWTUser;
use Auth0\Login\Contract\Auth0UserRepository;

/**
* Created by PhpStorm.
* User: germanlena
* Date: 4/20/15
* Time: 11:10 AM
*/

class Auth0UserRepository implements \Auth0\Login\Contract\Auth0UserRepository {
class Auth0UserRepository implements Auth0UserRepository {

public function getUserByDecodedJWT($jwt) {
return new Auth0JWTUser($jwt);
Expand Down

0 comments on commit 3bf1b17

Please sign in to comment.