PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 9, 2022

[FIXED] how to check users on login if its already activated

 January 09, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

users field, activation_key string, status int = 0 default,

activated profile will have status = 1 but it will always login even if the user is not yet activated,

If its already activated then users can login but if not yet activated put a message "please activate first'

assuming my activate method already done,

<?php
public function login()
  {
    if ($this->request->is('post')) {

        if (Validation::email($this->request->data['username'])) {
            $this->Auth->config('authenticate', [
                'Form' => [
                    'fields' => ['username' => 'email']
                ]
            ]);
            $this->Auth->constructAuthenticate();
            $this->request->data['email'] = $this->request->data['username'];
            unset($this->request->data['username']);
        }
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);

            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
}
?>

Solution

You can check status after authentication and before setUser().

Example

$user = $this->Auth->identify();

    if($user) {
     if($user['status'] == 0) { 
        $this->Flash->error(__('Your account is not activated yet.please check your email.'),array('class' => 'alert alert-danger'));
     } else {
       $this->Auth->setUser($user);
        //rest code here
     }
    }


Answered By - shubham715
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing