javascript - How do I get users to enter their username after logging in? -
i want users enter username before dashboard view (if it's first time ever logged in) , "enter unique username" appear once after first login , never appear again. i'm not sure involve homecontroller , dashboard view(timeline) or not , whether should @if , @else statement distinguish between username = null or not.
home controller:
use auth; class homecontroller extends controller { public function index() { if (auth::check()) { return view('dashboard.index'); } return view('home'); } }
this user.php:
use illuminate\foundation\auth\user authenticatable; class user extends authenticatable { protected $table = 'users'; protected $fillable = [ 'username', 'first_name', 'last_name', 'email', 'password', 'location', 'gender', 'date_of_birth', ]; protected $hidden = [ 'password', 'remember_token', ]; public function getname() { if ($this->first_name && $this->last_name) { return "{$this->first_name} {$this->last_name}"; } if ($this->first_name) { return $this->first_name; } return null; } public function getusername() { if ($this->first_name && $this->last_name) { return "{$this->first_name}.{$this->last_name}"; } if ($this->first_name) { return $this->first_name; } return null; } public function getnameorusername() { return $this->getname() ?: $this->username; } public function getusernameorname() { return $this->getusername() ?: $this->username; } public function getfirstnameorusername() { return $this->first_name ?: $this->username; } }
getusername.blade.php:
<div style="min-width: 800px; margin: 0 auto; position: relative; top: 75px;"> <div class="row" style="width: 600px; margin: 0 auto; border: 2px solid #000; padding: 40px;"> <div class="col-lg-6" style="width: 600px; color: #999; padding: 44.5px 0;"> <h3 style="color: #000; margin-top: 0; margin-bottom: 10px;">enter username</h3> <form class="form-vertical" role="form" method="post" action="{{ route('dashboard.getusername') }}"> <div> <div>your public username same profile address: <div> <ul> <li> <div>mostwanted.com/<span id="display_name"> <script> $('#username').keyup(function () { $('#display_name').text($(this).val()); }); </script> </span></div> </li> </ul> </div> </div> <div class="form-group {{ $errors->has('username') ? ' has-error' : '' }}"> <label for="username" class="control-label">choose username</label> <input style="width: 456px;" placeholder="e.g. {{ auth::user()->getusernameorname() }}" type="text" name="username" class="form-control" id="username" value=""> @if ($errors->has('username')) <span class="help-block" style="font-size: 12px; margin-bottom: 0;">{{ $errors->first('username') }}</span> @endif </div> <div>note: your username cannot changed , should include authentic name </div> </div> </div> <div class="form-group" style="margin-top: 15px;"> <button style="float: right;" type="submit" class="btn btn-primary">save username</button> </div> <input type="hidden" name="_token" value="{{session::token()}}"> </form> </div> </div>
usernamecontroller:
use auth; use db; use mostwanted\models\user; use illuminate\http\request; class usernamecontroller extends controller { public function getusername() { return view('dashboard.getusername'); } public function postusername(request $request) { $username = auth::user()->username; if (!$username) { return view('dashboard.getusername'); } $this->validate($request, [ 'username' => 'required|unique:users|regex:/^[a-za-z0- 9.]+$/|max:50', ]); user::create([ 'username' => $request->input('username'), ]); return view('dashboard.index'); } }
the last time did @if (auth::user()->username===null)
goes form entering username if i've entered username (redirect username form only).
p.s have no idea why <script>
doesn't work. want <span>
display whatever being entered in <input>
edit*:
routes.php:
/** *home + entering username */ route::get('/', [ 'uses' => '\mostwanted\http\controllers\homecontroller@index', 'as' => 'home', ]); route::get('/', ['middleware' => 'nousername', function () { [ 'uses' => '\mostwanted\http\controllers\usernamecontroller@getusername', 'as' => 'dashboard.getusername', ]; [ 'uses' => '\mostwanted\http\controllers\usernamecontroller@postusername', ]; }]); route::group(['middleware' => ['web']], function () { /** *authenication */ #sign route::get('/signup', [ 'uses' => '\mostwanted\http\controllers\authcontroller@getsignup', 'as' => 'auth.signup', 'middleware' => ['guest'], ]); route::post('/signup', [ 'uses' => '\mostwanted\http\controllers\authcontroller@postsignup', 'middleware' => ['guest'], ]); #log in route::get('/login', [ 'uses' => '\mostwanted\http\controllers\authcontroller@getsignin', 'as' => 'auth.signin', 'middleware' => ['guest'], ]); route::post('/login', [ 'uses' => '\mostwanted\http\controllers\authcontroller@postsignin', 'middleware' => ['guest'], ]); #log out route::get('/logout', [ 'uses' => '\mostwanted\http\controllers\authcontroller@getsignout', 'as' => 'auth.signout', ]); /** *search */ route::get('/search', [ 'uses' => '\mostwanted\http\controllers\searchcontroller@getresults', 'as' => 'search.results', ]); /** *user profile */ route::get('/user/{username}', [ 'uses' => '\mostwanted\http\controllers\profilecontroller@getprofile', 'as' => 'profile.index', ]); });
nousernamemiddleware:
namespace mostwanted\http\middleware; use auth; use closure; class nousernamemiddleware { public function handle($request, closure $next) { if(!auth::user()->username) { return redirect("/choose_username"); } return $next($request); } }
- you should use is_null function test username
- can give controller's method associated route('dashboard.getusername') ? maybe form works username not saved in database
Comments
Post a Comment