mtvbrianking / multi-auth

Laravel Multi-Authentication Package
https://github.com/mtvbrianking/multi-auth-diy
MIT License
179 stars 25 forks source link

Cannot login #17

Closed ashkan90 closed 5 years ago

ashkan90 commented 5 years ago

Hi, I cannot loginto other guarded model. When i log out and go back for log in again and bom. Credentials don't match. But register work like a boss << I'm trying to log in with username instead of email. <<

LoginController.php

protected function guard()
    {
        return Auth::guard('teacher');
    }

    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLoginForm()
    {
        return view('teacher.auth.login');
    }

    /**
     * Log the user out of the application.
     *
     * @param \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function logout(Request $request)
    {

        $this->guard()->logout();

        $request->session()->flush();

        $request->session()->regenerate();

        return redirect()->route('teacher.login');
    }

    public function username()
    {
        return "username";
    }
Teacher.php
<?php

namespace App;

use App\Notifications\Teacher\Auth\ResetPassword;
use App\Notifications\Teacher\Auth\VerifyEmail;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class Teacher extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'username', 'phone_number', 'permission', 'school_detail_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
         'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPassword($token));
    }

    /**
     * Send the email verification notification.
     *
     * @return void
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new VerifyEmail);
    }

    public function student(){
        return $this->belongsToMany('App\Student');
    }

    public function school_detail(){
        return $this->belongsToMany('App\SchoolDetail');
    }

    public function lessons(){
        return $this->belongsToMany('App\Lesson');
    }

    public function classes()
    {
        return $this->belongsToMany('App\Classes');
    }

    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }

    /*public function setSchool_detail_idAttribute()
    {
        $this->attributes['school_detail_id'] = Auth::guard('teacher')->user()->school_detail_id;
    }*/

    public function permission()
    {
        return $this->attributes['permission'];
    }

    public function dates(){
        return $this->hasMany('App\Date','teacher_id');
    }
}
Login.blade.php
@extends('teacher.layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Login') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('teacher.login') }}" aria-label="{{ __('Login') }}">
                        @csrf

                        <div class="form-group row">
                            <label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('Username') }}</label>

                            <div class="col-md-6">
                                <input id="username" type="text" class="form-control{{ $errors->has('username') ? ' is-invalid' : '' }}" name="username" value="{{ old('username') }}" required autofocus>

                                @if ($errors->has('username'))
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $errors->first('username') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

                                @if ($errors->has('password'))
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <div class="col-md-6 offset-md-4">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>

                                    <label class="form-check-label" for="remember">
                                        {{ __('Remember Me') }}
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-8 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Login') }}
                                </button>

                                @if (Route::has('teacher.password.request'))
                                    <a class="btn btn-link" href="{{ route('teacher.password.request') }}">
                                        {{ __('Forgot Your Password?') }}
                                    </a>
                                @endif
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
mtvbrianking commented 5 years ago

I'm trying to log in with username instead of email.

This package uses email by default as 'username'.

You can override the username() to achieve the desired behavior.

Source code

Documentation

Example

ashkan90 commented 5 years ago

I already write it.

public function username()
    {
        return "username";
    }

it's not working even in this case :(

mtvbrianking commented 5 years ago

Override the username() in app/Http/Controllers/Teacher/Auth/LoginController.php.

/**
 * {@inheritdoc}
 */
public function username()
{
-    return 'email';
+    return 'name';
}

Also, update the view (.../resources/views/teacher/auth/login.blade.php) to send name instead of email.

- <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
+ <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">

-    <label for="email" class="col-md-4 control-label">E-Mail Address</label>
+    <label for="name" class="col-md-4 control-label">Username</label>

    <div class="col-md-6">
-        <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
+        <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>

-        @if ($errors->has('email'))
+        @if ($errors->has('name'))
            <span class="help-block">
-                <strong>{{ $errors->first('email') }}</strong>
+                <strong>{{ $errors->first('name') }}</strong>
            </span>
        @endif
    </div>

</div>