라라벨(Laravel) 비밀번호 재설정 수정(Custom)

2020. 12. 28. 12:51php

라라벨에서 제공하는 기본 인증 기능을 사용하면 현재 사용하는 데이터스키마에 맞지 않아 수정이 필요함.

 

수정해야할 파일

Controllers\Auth\ForgotPasswordController.php

Controllers\Auth\ResetPasswordController.php

 

새로 생성해야할 파일

Providers\Passwords\CustomPasswordBroker.php

Providers\Passwords\CustomPasswordBrokerManager.php

Providers\Passwords\CustomPasswordResetServiceProvider.php

 

/**
 * 이메인 인증 키 변경
 */
public function credentials(Request $request)
{
	return ['mem_email' => $request->input('email')];
}

 

ResetPasswordController.php

/**
  * 이메일 인증 키 변경
  */
protected function credentials(Request $request)
{
  return [
      'mem_email' => $request->input('email'),
      'mem_password' => $request->input('password'),
      'password_confirmation' => $request->input('password_confirmation'),
      'token' => $request->input('token'),
  ];
}

/**
  * 비밀번호 변경 처리
  */
protected function setUserPassword($user, $password)
{
    $user->mem_password = Hash::make($password);
    $user->mem_password_updated_at = now();
}

 

CustomPasswordBroker

<?php

namespace App\Providers\Passwords;

use Closure;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Auth\Passwords\PasswordBroker as BasePasswordBroker;

class CustomPasswordBroker extends BasePasswordBroker
{
    /**
     * 비밀번호 재설정
     */
    public function reset(array $credentials, Closure $callback)
    {
        $user = $this->validateReset($credentials);
        
        // If the responses from the validate method is not a user instance, we will
        // assume that it is a redirect and simply return it from this method and
        // the user is properly redirected having an error message on the post.
        if (! $user instanceof CanResetPasswordContract) {
            return $user;
        }
        
        $password = $credentials['mem_password'];
        
        // Once the reset has been validated, we'll call the given callback with the
        // new password. This gives the user an opportunity to store the password
        // in their persistent storage. Then we'll delete the token and return.
        $callback($user, $password);
        
        $this->tokens->delete($user);
        
        return static::PASSWORD_RESET;
    }
}

CustomPasswordBrokerManager

라라벨 PasswordBrokerManager.php 클래스 내용을 전부 복사한다. resolve 함수는 아래 처럼 수정

    /**
     * Resolve the given broker.
     *
     * @param  string  $name
     * @return \Illuminate\Contracts\Auth\PasswordBroker
     *
     * @throws \InvalidArgumentException
     */
    protected function resolve($name)
    {
        $config = $this->getConfig($name);
        
        if (is_null($config)) {
            throw new InvalidArgumentException("Password resetter [{$name}] is not defined.");
        }
        
        // The password broker uses a token repository to validate tokens and send user
        // password e-mails, as well as validating that password reset process as an
        // aggregate service of sorts providing a convenient interface for resets.
        return new CustomPasswordBroker(
            $this->createTokenRepository($config),
            $this->app['auth']->createUserProvider($config['provider'] ?? null)
        );
    }

 

CustomPasswordResetServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Providers\Passwords\CustomPasswordBrokerManager;

class CustomPasswordResetServiceProvider extends ServiceProvider
{
    protected $defer = true;
    
    public function register()
    {
        $this->registerPasswordBrokerManager();
    }
    
    protected function registerPasswordBrokerManager()
    {
        $this->app->singleton('auth.password', function ($app) {
            return new CustomPasswordBrokerManager($app);
        });
    }
    
    public function provides()
    {
        return ['auth.password'];
    }
}

 

config\app.php 프로바이더 호출 부분 수정

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        // .....

        //  Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        App\Providers\Passwords\CustomPasswordResetServiceProvider::class,

        // .....

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */

        // .....

    ],

 

테스트 결과

 

 

 

이메일 전송 방법은 smtp 설정을 통해 간단하게 가능하고 여러저러 확장성 생각하면 그냥 새로 만드는게 좋을듯 싶음

 

 

참고 사이트

stackoverrun.com/ko/q/11160375

gist.github.com/JT501/1f800576d7ad23b3d29b378cf7051403

'php' 카테고리의 다른 글

Mac php xdebug 설치  (1) 2021.06.11
Codeigniter3 쇼핑몰 github  (0) 2021.06.06
Codeigniter3 Custom model github  (0) 2021.06.06
PHP 특수함수(Magic method)  (0) 2021.01.12
Codeigniter4 에서 Class Kint Not Found 오류 해결  (0) 2020.07.03