CodeIgniter 4 User Login Tutorial - Part 3 - User Login & Session

Поделиться
HTML-код
  • Опубликовано: 24 дек 2024

Комментарии • 125

  • @fuchandraxing4512
    @fuchandraxing4512 4 года назад +1

    hi @Alex Lancer,
    in your Route.php :
    $routes->get('/', 'Users::index');
    this is not work for me, I change to :
    $routes->match(['get', 'post'], '/', 'Users::index')
    and it's works for me.
    Is that right ?
    Please comment, thank you.

  • @gonzalo3327739
    @gonzalo3327739 2 года назад +1

    Hi alex, every time I try to log in with credential right or wrong I get the message 404 and it tells me that "Can't find route for 'post: /.' I checked your code in github and it was exactly the same, do you know why tjis happens?

    • @christopherneiltafurmorale135
      @christopherneiltafurmorale135 Год назад

      I got the same problem, did you manage to solve it?

    • @christopherneiltafurmorale135
      @christopherneiltafurmorale135 Год назад

      I found a solution for this from @fuchandraxing4512 :
      hi @Alex Lancer,
      in your Route.php :
      $routes->get('/', 'Users::index');
      this is not work for me, I change to :
      $routes->match(['get', 'post'], '/', 'Users::index')
      and it's works for me.

  • @sebastianramirez2266
    @sebastianramirez2266 4 года назад +2

    Hi Alex, When I put the data correctly to login, I keep getting the validation error(Email or Password don't match).

    • @AlexLancer
      @AlexLancer  4 года назад

      Did you insert data into database manually or through registration form?

    • @sebastianramirez2266
      @sebastianramirez2266 4 года назад

      @@AlexLancer through registration form

    • @iulianab
      @iulianab 3 года назад

      Hi! I have the same problem and I really don't know how to fix it.. Can you please tell me how you resolve it? I will be very thankful

    • @sharoonbhatti4386
      @sharoonbhatti4386 2 года назад +1

      @@iulianab I'm having the same problem. Did anyone finf solution to this? Need help!

    • @ritzbhu2917
      @ritzbhu2917 2 года назад

      same error. Its something to do with the validateUser method.

  • @tapanbhanot
    @tapanbhanot 4 года назад +3

    Hi, Thanks for the great tutorial. I am getting error
    CodeIgniter\Validation\Exceptions\ValidationException
    is not a valid rule.
    SYSTEMPATH\Validation\Exceptions\ValidationException.php at line 10
    How to fix ? I have check and I have typed everything same as yours but your is working and I get error!!!

  • @glennperete
    @glennperete 4 года назад +1

    Hi, I am at 22:17 into the video. When I log in, I am redirected to "localhost/login/dashboard" resulting in 404 error. But when I edit the address in firefox to "localhost/dashboard" the "Hello, Alex" message appears. Any tips on how I can fix this? Thanks

    • @AlexLancer
      @AlexLancer  4 года назад

      Hey Glen, check your redirect() method it should be redirect()->to('/dashboard') not redirect()->to('dashboard'). See the slash '/' before dashboard ?
      Tell me if that's the case.

    • @glennperete
      @glennperete 4 года назад +1

      @@AlexLancer Hi, I have changed it to "return redirect()->to('/dashboard');" and it is working now. Thanks. Just a heads up, in the video at 22:09 on line 37 it reads "return redirect()->to('dashboard');"

    • @AlexLancer
      @AlexLancer  4 года назад +2

      @@glennperete Yes because in my case it does not play any role ('dashboard') or ('/dashboard')
      But in your case, because you are on localhost/login (while in the video i am on localhost) you have to add '/' so that the app knows that you mean to redirect to the localhost/dashboard, if you dont add the slash it will concat the existing url + dashboard

    • @zhr.channel3325
      @zhr.channel3325 Год назад

      @@AlexLancer hii alex, I have changed (dashboard) to (/dashboard) still can't

  • @penguinstyles264
    @penguinstyles264 3 года назад +1

    Hey Alex! Great tutorial so far. I am having an issue at 11:05 where I am presented with a "The email field is required.
    The password field is required." instead of "Email or password don't match". I continued through and added the password_verify line to UserRules.php but I am still presented with "The email field is required.
    The password field is required." even though they are both supplied and correct. Any suggestions?

    • @alighieribosch
      @alighieribosch 3 года назад

      You must set id & name to the in the login.php view file.

  • @coleenjaemoralidad2473
    @coleenjaemoralidad2473 2 года назад

    help! i have an error "Call to undefined function set_value() " everytime I login.

  • @sivasankari5839
    @sivasankari5839 2 года назад

    hi sir , i followed your tutorial. how do you set default localhost in codeigniter? i am using like this localhost:8080/users because of its i cannot redirect to next page after submit. it send next page like localhost/users

  • @aqsalardiana951
    @aqsalardiana951 2 года назад

    i already followed your tutorial, but everytime i clicked that login button, it enters anyway even with random data (random username and password)

  • @dengannama8987
    @dengannama8987 4 года назад +1

    Hi Alex, Thank you for giving this very useful tutorial. but I found a problem with validateUser[email, password] which shows a mismatch. is this a problem with UserModel or UserRules? even though I've often compared your code.

    • @mienyu9016
      @mienyu9016 4 года назад

      i got same error

    • @AlexLancer
      @AlexLancer  4 года назад

      Muhammad, do you mean that when you provide submit correct email & pass you still get validation error? If so please upload your code to github so that I can check it.

    • @dengannama8987
      @dengannama8987 4 года назад

      @@AlexLancer I have solved it, thank you 😁

    • @AlexLancer
      @AlexLancer  4 года назад +1

      @@dengannama8987 Can you share what was causing it ?

    • @dengannama8987
      @dengannama8987 4 года назад

      @@AlexLancer I modified your codes into my model, and it's works 😅

  • @tomyhawks9344
    @tomyhawks9344 4 года назад +1

    I got an interesting error bro. When i add the new code on my style.css did not load to html.
    First i went to page i inspect the link and the path was okay but showing just the
    body{
    background-color: #e1e1e1;
    }
    without the new added code.
    Then i create a second css -> style2.css with the whole new code and it passed succesfully.
    But i can't understand why the new code is not loading at style.css

    • @AlexLancer
      @AlexLancer  4 года назад

      First of all thanks for watching. Now you know it can be a million things. 1st that comes to mind is browser caching. Maybe it got cached with an error and then you could not clear it. Also, are you using XAMPP or some live server for your development?

    • @tomyhawks9344
      @tomyhawks9344 4 года назад

      @@AlexLancer Well i have to thank you for spending your time to teach us. Well i use XAMPP for my development i tried also to stackoveflow it but the only thing that i found it was "checking the link" in my case link is okay. i doubt alson on broswer caching. Thanks for the fast replay.

    • @AlexLancer
      @AlexLancer  4 года назад

      if you could share your code somewhere, it would help. Can you ?

  • @mrandmrsbhatia479
    @mrandmrsbhatia479 4 года назад +2

    Hi Thanks for great video but password verify function not returning anything..its always says that not match...am stuck here please help

    • @IRSHADJABBAR
      @IRSHADJABBAR 3 года назад

      @jasneet bhatia - did you get resolved? i have the same issue for the lock screen with pin . however user login is working perfectly. Thanks Alex for your great contributions and appreciated for the tutorials.

    • @mrandmrsbhatia479
      @mrandmrsbhatia479 3 года назад

      @@IRSHADJABBAR yes it was done

    • @IRSHADJABBAR
      @IRSHADJABBAR 3 года назад

      @@mrandmrsbhatia479 what changes you made

  • @zhr.channel3325
    @zhr.channel3325 Год назад

    Hi axe , can you help me please
    404
    Can't find a route for 'post: /

  • @prithvituladhar6938
    @prithvituladhar6938 3 года назад

    validateUser function has error which allows incorrect password to login.....there is no else statement of " if(!$user) ".

  • @AntonioVillordo
    @AntonioVillordo 4 года назад

    Excelente Axel... la libreria Shopping Cart , no esta disponible en esta version? Sabes si existe alguna posibilidad de integrarla? Gracias

    • @AlexLancer
      @AlexLancer  4 года назад

      Hi Antonio, yes shopping card has been removed from Codeigniter 4. You can use other external libraries for that. I don't have a specific suggestion.

    • @AntonioVillordo
      @AntonioVillordo 4 года назад

      @@AlexLancer Thank you Alex.

  • @windy112233
    @windy112233 4 года назад

    Alex - Great tutorial!
    I have a problem with picking up the custom css for the error messages on Login/Registration forms. I am adding the following to the head section (right under the bootstrap link) of Views/templates/header.php file: and there is a file by that name in that location, but I can't seem to access the styles. Any thoughts?

    • @profragnar4514
      @profragnar4514 3 года назад

      Facing the same problem, renamed it to "main.css" and it worked.

  • @itsRekx
    @itsRekx 4 года назад

    I was wondering and a little bit confused, you use CodeIgniter/Model in the model file but don't use CodeIgniter/Controller in the controller file. So what's the difference if using CodeIgniter/Controller instead to not using it? isn't that important?

    • @AlexLancer
      @AlexLancer  4 года назад +1

      Hey, Rifat,
      All of the controllers extend BaseController. If you go inside BaseController you will see that it extends Controller which is imported through "use Condigniter\Controller"
      So because my controllers extend BaseController it inherits the Codeigniter Controller from there. But good question! Well done!

    • @itsRekx
      @itsRekx 4 года назад

      @@AlexLancer Woah, I didn't pay attention to that! Thanks for the explanation. I am waiting for your next video. Can you explain about creating a progressive web application using CI4 for the next video? I always look for CI3 but I don't get a clear explanation. By the way, thank you very much!

  • @kumarpal8837
    @kumarpal8837 4 года назад

    hi Alex i have another issue i tried it lot but still not getting the clue why it is happening it is on git too on same repo also .what i try to achive is when user check the remember me in login page and submit in controller i set cookie with token and same token i want to update in database but update query not working with active record syntax.here is the code where i am stuck now
    if($this->request->getPost('remember')){
    $token = random_string('md5', 32);
    set_cookie('remembermetoken', $token,'3600'); // with cookies working code

    $User->set('remember_token',$token);
    $User->where('email',$this->request->getPost('email'));
    $User->update();
    // the above three line of code produce
    //UPDATE `user` SET = '' WHERE `email` = 'pal@gmail.com'
    // why it is not setting remember_token
    //contrary to that if i use
    // $User->query("UPDATE `user` SET `remember_token`='".$token."' WHERE `email`= '".$this->request->getPost('email')."'");
    // it will produce correct query ?
    //UPDATE `user` SET `remember_token`='d0dc17846d297bb28ac37e16e280060d' WHERE `email`= 'pal@gmail.com'
    echo $User->getLastQuery();
    die;

    }

    • @AlexLancer
      @AlexLancer  4 года назад

      Hi Kumar, try
      Because you are using the CodeIgniter Model class for your UserModel
      the update works like this
      $User->update($id, $data);
      $data is your data that you want to save. it is an array.
      $data = ['remember_token' => $token];
      $id - is you user's id
      The way you used it in your code it would work if you would use it like:
      $db = db_connect();
      $builder = $db->table('users');
      $builder->set(bla bla)->where->(bla bla)->update();

  • @siddheshkadam2138
    @siddheshkadam2138 4 года назад

    Hey hie Alex, I just want to validate session for all controllers. How can I do with CI4?
    Plz guide

  • @kumarpal8837
    @kumarpal8837 4 года назад

    Still facing the same issue Alex used this code as you said
    $newdata=['remember_token'=>$token];
    $User->update($userdata['id'],$newdata);
    Now it produce this result
    UPDATE `user` SET = '' WHERE `user`.`id` IN ('5') still not setting the remember_me field in query checked my allowedfield in UserModel also there is remember_me in array but still not working dont understand what i m doing wrong same kind of code is working with recoverpassword() function of User controller but not here

    • @AlexLancer
      @AlexLancer  4 года назад

      The query that is being generated does not look correct

    • @kumarpal8837
      @kumarpal8837 4 года назад

      yes it is not producing the correct query . but why it is not producing the correct query this was the issue.

    • @kumarpal8837
      @kumarpal8837 4 года назад

      it is solved thanks. In callback function in UserModel . i am not returning anything that is why it was not setting the data in query now done thanks again

    • @AlexLancer
      @AlexLancer  4 года назад

      @@kumarpal8837 ooff, finally!

  • @lifejoin7742
    @lifejoin7742 3 года назад

    Sir
    It would be great if you or some one reply me
    I have set session at local storage with browse close session destroy
    And I m running my project in local server
    With some ip 192.168.67.60/ci4
    Some time session works properly for long time
    But sometimes suddenly session is destroy and user are logout
    Pls let me know
    Any settings

  • @witfproject
    @witfproject 3 года назад

    I love this video and the content . I'll go subscribe and turn notification so i can get notif if you publish another video. Keep the great work man. Good luck.

  • @alejandrorosales
    @alejandrorosales 4 года назад

    Argument 1 passed to App\Validation\UserRules::validateUser() must be of the type string, null given, called in
    public function validateUser(string $str, string $fields, array $data)
    the stranger thing is: works twice! in thirth try show previous error... i dont know why...

  • @iulianab
    @iulianab 3 года назад

    Hi! First, I want to thank you for what are you doing on your channel. It helps me, but now I have a problem with validateUser method, even if the password is correct , the form is not valid because the password and email don't mach. Can you please provide me a solution? Thank you in advance!

    • @plain3808
      @plain3808 3 года назад

      hello! did you find a solution? im expriencing the same problem

    • @sharoonbhatti4386
      @sharoonbhatti4386 2 года назад

      @@plain3808 Did you find the solution to that, Now I'm having the same problem after watching the tutorial for all day. Stuck here.

  • @BhuvneshMaheta
    @BhuvneshMaheta 4 года назад

    Unable to get session value in another controller, Please help.

    • @AlexLancer
      @AlexLancer  4 года назад

      Hey Bhuvnesh, upload your code on gihub and describe the issue in detail please.

  • @shivans9456
    @shivans9456 4 года назад

    I will download your project files in GitHub and installed it then I chake it I will insert right password but he should display error email or password is not right

    • @shivans9456
      @shivans9456 4 года назад

      Sollu this error and reply me fast

  • @Xvqier
    @Xvqier 4 года назад

    Hi Alex, I see you face an error on the line 33 because of the $model->model() whereas it should be $model->where() which I am not sure why did you only get the error now and not when you test the login function earlier. I should have just continued watching the whole video instead of spending 2 days to debug that issue. After I fixed it then I realized you had that error. omg

  • @jakovnikolaus2172
    @jakovnikolaus2172 4 года назад +1

    I'm using your code, and when I try to login I'm getting "Email or Password don't match" is not a valid rule error. Please help!

    • @princeraj9536
      @princeraj9536 4 года назад

      Remove 'is_unique[users.email]' from the $rules and try it again . It will work

    • @rarciniegas
      @rarciniegas 4 года назад +1

      I had the same error and was caused by the password field set to varchar(50). I change it to varchar(255) and solved the problem.
      Alex your are a great instructor, Thanks a lot
      👍👍👍👍👍

    • @sharoonbhatti4386
      @sharoonbhatti4386 2 года назад +1

      @@rarciniegas Thnaks Your comment solved my problem.

  • @kumarpal8837
    @kumarpal8837 4 года назад

    Alex first of all thanks for sharing such precious Knowledge with the world i am following your playlist of login registration and try to extend it bit by myself by involving forget password but i am having trouble in setting the cookies in ci4 here is bit sample code from my controller hope you get it
    helper('cookie');
    $this->response->set_cookie('forgetpwd',$token, time() + (3600),"/",site_url());
    print_r($this->request->get_cookie('forgetpwd'));
    die();
    but it output blank

    • @AlexLancer
      @AlexLancer  4 года назад

      Hi, Kumar, thank for your kind words. Please upload your code to github so that i can analyze it.
      And also why do you use a cookie for 'Forgot password' ? What do you try to achieve?
      Thanks.

    • @AlexLancer
      @AlexLancer  4 года назад

      try using set_cookie(optione here) without $this->response->set_cookie()

    • @kumarpal8837
      @kumarpal8837 4 года назад

      thanks it works thanks a lot Alex please make some community where we can discuss all such issues so that all of us can contribute to your efforts in CodeIgniter 4 related issue and discussions related to it

    • @AlexLancer
      @AlexLancer  4 года назад

      @@kumarpal8837 Thanks for the suggestion. For now the focus is to establish the community on RUclips. So please suggest my channel to others or share a video on your channel. That would be a great support for me. Thanks!

  • @JohChaser
    @JohChaser 11 месяцев назад

    email password dont match event with correct email and pass

  • @mienyu9016
    @mienyu9016 4 года назад +1

    Im getting error "Class 'CodeIgniter\Validation\UserRules' not found" please fast response

    • @AlexLancer
      @AlexLancer  4 года назад +1

      Download the source from the github (check description) and compare your code. You 've missed something

    • @mienyu9016
      @mienyu9016 4 года назад

      ​@@AlexLancer I am confused now. I will contact you immediately if this problem still cannot be resolved.

    • @mienyu9016
      @mienyu9016 4 года назад

      @@AlexLancer My mistake in "\App\Validation\UserRules::class", I am not check the path. Then the problem is when I type correct email and password look like in the database, it throw error "Email or Password don't match". I wish I could solve this small problem. Thank you for giving this great tutorial. Have a nice day :)

    • @AlexLancer
      @AlexLancer  4 года назад

      post your code on github please.

    • @mienyu9016
      @mienyu9016 4 года назад

      @@AlexLancer It has been resolved. It caused by bcrypt password. Is your code using auto bcrypt password for new user register? And
      add them into database. All user in my database table haven't password yet. I was trying make new user password from phpmyadmin without encrypted and i got these mismatch. However now I have to bcrypt password then paste it to user table.

  • @shanid-m2e
    @shanid-m2e 4 года назад

    login with different passwords is working.

    • @doomhaMwx
      @doomhaMwx 3 года назад

      Apparently, I'm having the same issue. How did you fix it? Thank you in advance.

  • @sandeepkumarr1212
    @sandeepkumarr1212 4 года назад

    a great thank you to you for this help

    • @AlexLancer
      @AlexLancer  4 года назад

      You're welcome, Sandeep!

    • @sandeepkumarr1212
      @sandeepkumarr1212 4 года назад

      sir can we a create a single model file for multiple tables as old version

    • @AlexLancer
      @AlexLancer  4 года назад

      @@sandeepkumarr1212 watch this video's intro starting at 1:00 shorturl.at/ayT59
      and then go to 50:30 of the same video to see how to make a CustomModel (similar to CI3)

  • @kumarpal8837
    @kumarpal8837 4 года назад

    Thanks for Your reply on the matter. Here is the git link github.com/stackpalk/Codeigniter4.git there is .sql file also on root .What i try to achieve is simple i try to implement forget pwd functionality on first screen i send mail by taking email id of user with newly generated token which is valid for 1 hour only that's why i need to set cookie in browser to compare it with the token that i send in email to proceed further similarly cookies can be used to remember me at login screen by setting token and encrypted credential for the next login automatically which is the next task i am going to implement.Plz configure email by your credential. Thanks for your efforts and time Alex

    • @AlexLancer
      @AlexLancer  4 года назад

      try using set_cookie(optione here) without $this->response->set_cookie()

  • @gim-ori
    @gim-ori 3 года назад

    You are my hero -_-)b

  • @PwrSrg
    @PwrSrg 4 года назад +1

    Not bad, but NOT good.
    The biggest issue is that he doesn't explain ANYTHING he is doing, NOR WHY. It is like he is reading off of a script on another monitor, and already expects you to know the framework, keywords, methods, etc. It feels like he forgets that this is a Ci4 TUTORIAL, and NOT a HOW TO video. Or am I mistaken??
    He also spends a LOT of time writing line after line of code without showing WHAT the code is actually doing as he writes it. Which is VERY difficult to learn by. One of the videos goes almost the entire length before testing it! A proper tutorial shows you progress every few minutes.
    He also makes TONS of mistakes and typos, that are easily caught right away, and then have to fast forward through later when he goes back to fix them. That is easily the most frustrating part.
    Bottom line - You really already need to be a PHP EXPERT, and FLUENT in Codeigniter 4 before watching this series. Otherwise you will be LOST. Too bad since not many RUclipsrs are covering Ci4 yet.

    • @davidfyork
      @davidfyork 3 года назад

      Friendly suggestion for you Sergio... Perhaps watch Alex’s series “CodeIgniter 4 from Scratch” first, then watch this series. You might feel differently about it then. I agree he makes typos, but he’s only human, we all do typos, and he shows how he spots them and corrects them, which is actually useful to a beginner.

    • @AlexLancer
      @AlexLancer  3 года назад +1

      Hey, Sergio, thanks for taking time to write the comment. I will take into consideration everything you say.

  • @jigglypotatoes
    @jigglypotatoes 4 года назад

    Hi Alex, I discovered something interesting. In your video you outputted errors to view by using $data['validation'] = $this->validator; that way the whole validation object gets passed to view with all the rules and stuff. It might be much leaner by passing just the error array by doing $data['errors'] = $this->validator->geterrors(); Then In the view you can traverse through $errors array of key value pairs.

    • @AlexLancer
      @AlexLancer  4 года назад +1

      Bartlomiej, glad that you keep learning. Yes you are totally correct.
      The difference is that the errors I output are already with some predefined HTML, i believe as a UL->LI items. And actually you can create your own templates and then use it without foreach loop.
      But I also tend to use the getErrors method.

  • @jigglypotatoes
    @jigglypotatoes 4 года назад

    Alex, great tutorial! I have tried to make validation in the model for my registration view do i need to create seperate model for my login view now since validation will change? How do I create custom validation rule in the model?

    • @jigglypotatoes
      @jigglypotatoes 4 года назад

      Thinking about it still I'm wondering do we have to validate login credentials at all? after all they are not being saved anywhere just read and compared. simple html5 validation could suffice on a front-end side of things imho.

    • @AlexLancer
      @AlexLancer  4 года назад +1

      You can not rely on frontend validation at any point. Only backend validation.
      Now, the custom validation rule, can be just a simple method in your controller (or model) you don't need to make it a rule, as I showed in the tutorial, that was just an example, you can just create a method insider your file that will check that.

    • @jigglypotatoes
      @jigglypotatoes 4 года назад

      @@AlexLancer In that case would simple if else statement suffice? Like check if email and pass is there then if it is then if there is user with that email, compare pass if they match log in. anything else error? Looking at it ... it is what validation does anyways.

    • @AlexLancer
      @AlexLancer  4 года назад +1

      @@jigglypotatoes yes, thats exactly what i said to you in previous reply. You can add it wherever you like. No right or wrong.

    • @jigglypotatoes
      @jigglypotatoes 4 года назад

      @@AlexLancer Thank you sorry for repeating it twice like this. I would want to reach the stage when i will be more productive and confident with writing code. Some days I write one line and I try to figure out what to do next and how to. How long did it take you to reach your level of proficiency?

  • @coleenjaemoralidad2473
    @coleenjaemoralidad2473 2 года назад

    help! i have an error "Call to undefined function set_value() " everytime I login.

    • @AlexLancer
      @AlexLancer  2 года назад

      I guess you have not loaded the 'form' helper before using set_value()