Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

Laravel 5.0 के साथ एक रेस्टफुल पासवर्ड रिमाइंडर कैसे बनाएं और यूजर ईमेल फील्ड को यूजरनेम फील्ड में कैसे बदलें?

उपयोगकर्ता मॉडल पर getEmailForPasswordReset विधि सेट करने से समस्या हल हो जाती है:

<?php
public function getEmailForPasswordReset() {
    return $this->username;
}

और मेरा पासवर्ड नियंत्रक ईमेल फ़ील्ड के बजाय POST/पासवर्ड/उपयोगकर्ता नाम/{टोकन} फ़ील्ड के साथ काम करने के लिए:

<?php namespace Reverse\Http\Controllers\Auth;

use Illuminate\Http\Request;
use Reverse\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Foundation\Auth\ResetsPasswords;

class PasswordController extends Controller {

    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset requests
    | and uses a simple trait to include this behavior. You're free to
    | explore this trait and override any methods you wish to tweak.
    |
    */

    use ResetsPasswords;

    /**
     * Create a new password controller instance.
     *
     * @param  \Illuminate\Contracts\Auth\Guard  $auth
     * @param  \Illuminate\Contracts\Auth\PasswordBroker  $passwords
     * @return void
     */
    public function __construct(Guard $auth, PasswordBroker $passwords)
    {
        $this->auth = $auth;
        $this->passwords = $passwords;

        // With this, when logged says: "You're logged!" and not send the email token
        //$this->middleware('guest');
    }

    /**
     * Send a reset link to the given user.
     *
     * @param  Request  $request
     * @return Response
     */
    public function postUsername(Request $request)
    {
        $validator = \Validator::make(
            ['username' => $request->get('username')],
            ['username' => 'required|email|min:6|max:255']
        );

        if($validator->passes()) {
            $response = $this->passwords->sendResetLink($request->only('username'), function ($m) {
                $m->subject($this->getEmailSubject());
            });

            switch ($response) {
                case PasswordBroker::RESET_LINK_SENT:
                    return \Response::json(['success' => 'true']);
                    //return redirect()->back()->with('status', trans($response));

                case PasswordBroker::INVALID_USER:
                    return \Response::json(['success' => 'true', 'status' => trans($response)]);
                    //return redirect()->back()->withErrors(['username' => trans($response)]);
            }
        } else {
            return \Response::json(['error' => [
                'messages' => $validator->getMessageBag(),
                'rules' => $validator->getRules()
            ]]);
        }
    }

    /**
     * Reset the given user's password.
     *
     * @param  Request  $request
     * @return Response
     */
    public function postReset(Request $request)
    {
        $this->validate($request, [
            'token' => 'required',
            'username' => 'required|email|min:6|max:255',
            'password' => 'required|confirmed',
        ]);

        $credentials = $request->only(
            'username', 'password', 'password_confirmation', 'token'
        );

        $response = $this->passwords->reset($credentials, function($user, $password)
        {
            $user->password = bcrypt($password);
            $user->save();
            $this->auth->login($user);
        });

        switch ($response)
        {
            case PasswordBroker::PASSWORD_RESET:
                return \Response::json(['success' => 'true']);
                //return redirect($this->redirectPath());

            default:
                return \Response::json(['success' => 'false', 'status' => trans($response)]);
                /*return redirect()->back()
                    ->withInput($request->only('email'))
                    ->withErrors(['email' => trans($response)]);*/
        }
    }

}

मैं रीसेट दृश्य भी बदलता हूं...

// /resources/views/auth/reset.blade.php
<input type="email" class="form-control" name="username" value="{{ old('username') }}">

मैं .env कॉन्फ़िगरेशन फ़ाइल पर एक नए SMTP_HOST चर जैसे env चर से उपयोग करने के लिए अपनी /config/mail.php कॉन्फ़िगरेशन फ़ाइल बदलता हूं:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Mail Driver
    |--------------------------------------------------------------------------
    |
    | Laravel supports both SMTP and PHP's "mail" function as drivers for the
    | sending of e-mail. You may specify which one you're using throughout
    | your application here. By default, Laravel is setup for SMTP mail.
    |
    | Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "log"
    |
    */

    'driver' => env('MAIL_DRIVER', 'smtp'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Address
    |--------------------------------------------------------------------------
    |
    | Here you may provide the host address of the SMTP server used by your
    | applications. A default option is provided that is compatible with
    | the Mailgun mail service which will provide reliable deliveries.
    |
    */

    'host' => env('SMTP_HOST', 'smtp.mailgun.org'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Port
    |--------------------------------------------------------------------------
    |
    | This is the SMTP port used by your application to deliver e-mails to
    | users of the application. Like the host we have set this value to
    | stay compatible with the Mailgun e-mail application by default.
    |
    */

    'port' => env('SMTP_PORT', 587),

    /*
    |--------------------------------------------------------------------------
    | Global "From" Address
    |--------------------------------------------------------------------------
    |
    | You may wish for all e-mails sent by your application to be sent from
    | the same address. Here, you may specify a name and address that is
    | used globally for all e-mails that are sent by your application.
    |
    */

    'from' => [
        'address' => env('MAIL_FROM_DEFAULT', '[email protected]'),
        'name' => env('MAIL_NAME_DEFAULT', 'Admin')
    ],

    /*
    |--------------------------------------------------------------------------
    | E-Mail Encryption Protocol
    |--------------------------------------------------------------------------
    |
    | Here you may specify the encryption protocol that should be used when
    | the application send e-mail messages. A sensible default using the
    | transport layer security protocol should provide great security.
    |
    */

    'encryption' => env('SMTP_ENCRYPTION', 'tls'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Server Username
    |--------------------------------------------------------------------------
    |
    | If your SMTP server requires a username for authentication, you should
    | set it here. This will get used to authenticate with your server on
    | connection. You may also set the "password" value below this one.
    |
    */

    'username' => env('SMTP_USERNAME', null),

    /*
    |--------------------------------------------------------------------------
    | SMTP Server Password
    |--------------------------------------------------------------------------
    |
    | Here you may set the password required by your SMTP server to send out
    | messages from your application. This will be given to the server on
    | connection so that the application will be able to send messages.
    |
    */

    'password' => env('SMTP_PASSWORD', null),

    /*
    |--------------------------------------------------------------------------
    | Sendmail System Path
    |--------------------------------------------------------------------------
    |
    | When using the "sendmail" driver to send e-mails, we will need to know
    | the path to where Sendmail lives on this server. A default path has
    | been provided here, which will work well on most of your systems.
    |
    */

    'sendmail' => '/usr/sbin/sendmail -bs',

    /*
    |--------------------------------------------------------------------------
    | Mail "Pretend"
    |--------------------------------------------------------------------------
    |
    | When this option is enabled, e-mail will not actually be sent over the
    | web and will instead be written to your application's logs files so
    | you may inspect the message. This is great for local development.
    |
    */

    'pretend' => false,

];

और अब, ठीक काम करता है!




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL अपवाद हैंडलर एक्सेस अपवाद को संभाला जा रहा है

  2. फ्लास्क SQLAlchemy के साथ SQLite से MySQL में स्विच करना

  3. क्लास डेटटाइम का ऑब्जेक्ट स्ट्रिंग में परिवर्तित नहीं किया जा सका

  4. MySQL और हिम तेंदुए के साथ बार-बार परेशानी होने पर - लॉक करने में असमर्थ ./ibdata1, त्रुटि:35

  5. php . का उपयोग करके mysql तालिका में टाइमस्टैम्प सहेजा जा रहा है