Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

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

Avatar for moyashidaisuke

moyashidaisuke

March 31, 2018
Tweet

More Decks by moyashidaisuke

Other Decks in Technology

Transcript

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

    ΈΜΕͼ • αʔόαΠυΑΓͰ͕͢ϑϩϯτ΋ ΍Γ·͢ɻ • ڈ೥·Ͱ͸Ֆค঱͡Όͳ͔ͬͨ
  2. σϑΥϧτͷ࣮૷ΛݟΑ͏ 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Λฦ͢
  3. σϑΥϧτͷ࣮૷ΛݟΑ͏ 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Λฦ͢
  4. σϑΥϧτͷ࣮૷ΛݟΑ͏ 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Λड͚औͬͯߋ৽͢ Δ
  5. σϑΥϧτͷ࣮૷ΛݟΑ͏ 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Λฦ͢
  6. σϑΥϧτͷ࣮૷ΛݟΑ͏ 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ͷύεϫʔυͱҾ਺ ͷύεϫʔυΛνΣοΫ
  7. σϑΥϧτͷ࣮૷ΛݟΑ͏ retrieveById /** * {@inheritdoc} */ public function retrieveById($identifier) {

    return $this->repository->getUser($identifier); } JEFOUJpFSʢJEతͳʣΛड͚औͬͯ.PEFMΛ ฦ͢ʢ"1*ܦ༝ʣ
  8. σϑΥϧτͷ࣮૷ΛݟΑ͏ retrieveByToken /** * {@inheritdoc} */ public function retrieveByToken($identifier, $token)

    { return $this->repository->getUser($identifier, $token); } JEΛݩʹऔಘͯ͠UPLFOͱɺҾ਺ͷUPLFOΛൺ ֱͯ͠ɺҰகͯͨ͠Β.PEFMΛฦ͢ ʢ"1*ܦ༝ʣ
  9. σϑΥϧτͷ࣮૷ΛݟΑ͏ 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͠ͳ ͍ʣ࢓༷ͳͷͰಛʹແ͠
  10. σϑΥϧτͷ࣮૷ΛݟΑ͏ retrieveByCredentials /** * ೝূॲཧ * SessionGuard.attempt͔Βݺ͹ΕΔ * {@inheritdoc} */

    public function retrieveByCredentials(array $credentials) { return $this->userService->auth($credentials); } ͍ΘΏΔύεϫʔυೝূͯ͠.PEFMΛฦ͢ ʢ"1*ܦ༝ʣ ࠓճ͸ࣄલʹൃߦ͞Εͨ0OFUJNFͰೝূ
  11. σϑΥϧτͷ࣮૷ΛݟΑ͏ validateCredentials /** * ೝূॲཧ * SessionGuard.attempt͔Βݺ͹ΕΔ * retrieveByCredentialsͷޙʹݺ͹ΕΔ *

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