20180315これで簡単Laravelの認証処理をカスタマイズ

 20180315これで簡単Laravelの認証処理をカスタマイズ

3b0446884fc47b25b4bc2c8b06f97a24?s=128

moyashidaisuke

March 31, 2018
Tweet

Transcript

  1. 2.

    ࣗݾ঺հ • ͸͡Ί·ͯ͠ • ෱ాେཌྷʢ;͍͚ͩͩ͘͢ʣ • @moyashidaisuke • ίϩϓϥ ->

    ΈΜΕͼ • αʔόαΠυΑΓͰ͕͢ϑϩϯτ΋ ΍Γ·͢ɻ • ڈ೥·Ͱ͸Ֆค঱͡Όͳ͔ͬͨ
  2. 3.
  3. 5.
  4. 11.
  5. 14.
  6. 19.
  7. 22.

    σϑΥϧτͷ࣮૷ΛݟΑ͏ retrieveById /** * Retrieve a user by their unique

    identifier. * * @param mixed $identifier * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveById($identifier) { $model = $this->createModel(); return $model->newQuery() ->where($model->getAuthIdentifierName(), $identifier) ->first(); } JEFOUJpFSʢJEతͳʣΛड͚ औͬͯ.PEFMΛฦ͢
  8. 23.

    σϑΥϧτͷ࣮૷ΛݟΑ͏ retrieveByToken /** * Retrieve a user by their unique

    identifier and "remember me" token. * * @param mixed $identifier * @param string $token * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByToken($identifier, $token) { $model = $this->createModel(); $model = $model->where($model->getAuthIdentifierName(), $identifier)->first(); if (! $model) { return null; } $rememberToken = $model->getRememberToken(); return $rememberToken && hash_equals($rememberToken, $token) ? $model : null; } JEΛݩʹऔಘͯ͠UPLFOͱɺ Ҿ਺ͷUPLFOΛൺֱͯ͠ɺ Ұகͯͨ͠Β.PEFMΛฦ͢
  9. 24.

    σϑΥϧτͷ࣮૷ΛݟΑ͏ updateRememberToken /** * Update the "remember me" token for

    the given user in storage. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string $token * @return void */ public function updateRememberToken(UserContract $user, $token) { $user->setRememberToken($token); $timestamps = $user->timestamps; $user->timestamps = false; $user->save(); $user->timestamps = $timestamps; } UPLFOΛड͚औͬͯߋ৽͢ Δ
  10. 25.

    σϑΥϧτͷ࣮૷ΛݟΑ͏ retrieveByCredentials /** * Retrieve a user by the given

    credentials. * * @param array $credentials * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByCredentials(array $credentials) { if (empty($credentials) || (count($credentials) === 1 && array_key_exists('password', $credentials))) { return; } // First we will add each credential element to the query as a where clause. // Then we can execute the query and, if we found a user, return it in a // Eloquent User "model" that will be utilized by the Guard instances. $query = $this->createModel()->newQuery(); foreach ($credentials as $key => $value) { if (! Str::contains($key, 'password')) { $query->where($key, $value); } } return $query->first(); } ͍ΘΏΔύεϫʔυೝূ͠ ͯ.PEFMΛฦ͢
  11. 26.

    σϑΥϧτͷ࣮૷ΛݟΑ͏ validateCredentials /** * Validate a user against the given

    credentials. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param array $credentials * @return bool */ public function validateCredentials(UserContract $user, array $credentials) { $plain = $credentials['password']; return $this->hasher->check($plain, $user->getAuthPassword()); } .PEFMͷύεϫʔυͱҾ਺ ͷύεϫʔυΛνΣοΫ
  12. 28.

    σϑΥϧτͷ࣮૷ΛݟΑ͏ retrieveById /** * {@inheritdoc} */ public function retrieveById($identifier) {

    return $this->repository->getUser($identifier); } JEFOUJpFSʢJEతͳʣΛड͚औͬͯ.PEFMΛ ฦ͢ʢ"1*ܦ༝ʣ
  13. 29.

    σϑΥϧτͷ࣮૷ΛݟΑ͏ retrieveByToken /** * {@inheritdoc} */ public function retrieveByToken($identifier, $token)

    { return $this->repository->getUser($identifier, $token); } JEΛݩʹऔಘͯ͠UPLFOͱɺҾ਺ͷUPLFOΛൺ ֱͯ͠ɺҰகͯͨ͠Β.PEFMΛฦ͢ ʢ"1*ܦ༝ʣ
  14. 30.

    σϑΥϧτͷ࣮૷ΛݟΑ͏ updateRememberToken /** * Update the "remember me" token for

    the given user in storage. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string $token * @return void */ public function updateRememberToken(UserContract $user, $token) { $user->setRememberToken($token); } ࠓճ͸5PLFOΛӬଓԽ͠ͳ ͍ʢSFNFNCFSNF͠ͳ ͍ʣ࢓༷ͳͷͰಛʹແ͠
  15. 31.

    σϑΥϧτͷ࣮૷ΛݟΑ͏ retrieveByCredentials /** * ೝূॲཧ * SessionGuard.attempt͔Βݺ͹ΕΔ * {@inheritdoc} */

    public function retrieveByCredentials(array $credentials) { return $this->userService->auth($credentials); } ͍ΘΏΔύεϫʔυೝূͯ͠.PEFMΛฦ͢ ʢ"1*ܦ༝ʣ ࠓճ͸ࣄલʹൃߦ͞Εͨ0OFUJNFͰೝূ
  16. 32.

    σϑΥϧτͷ࣮૷ΛݟΑ͏ validateCredentials /** * ೝূॲཧ * SessionGuard.attempt͔Βݺ͹ΕΔ * retrieveByCredentialsͷޙʹݺ͹ΕΔ *

    ύεϫʔυೝূ͠ͳ͍ͷͰԿ΋͠ͳ͍ * {@inheritdoc} */ public function validateCredentials(Authenticatable $user, array $credentials): bool { return true; } .PEFMͷύεϫʔυͱҾ਺ͷύε ϫʔυΛνΣοΫ ࠓճ͸ύεϫʔυͳ͍ͷͰෆཁ
  17. 34.
  18. 35.
  19. 37.

    QA