Vue.js User Authentication with PHP

In this post we will show you Best way to implement user authentication using Vuejs php mysql, hear for Vuejs insert update delete using php mysqli with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Vue.js User Registration and Sign up with PHP/MySQLi

In this Example,First of all Add or Include External Libs Like as a(jQuery, css etc..), and then create a simple index.php or index.html page.After that crate a simple javascript file like as a index.js or main.js, It is also add your web-application First Header Part to some priority set.After that Include your relevant CSS Class.

Creating Simple Database using MySql

CREATE TABLE `Clients` (
`clientId` INT(11) NOT NULL AUTO_INCREMENT,
`email` VARCHAR(150) NOT NULL,
`password` VARCHAR(50) NOT NULL,
PRIMARY KEY(`clientId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

index.php

<!DOCTYPE html>
<html>
<head>
<title>Example of the Vue.js simple Registration form with PHP and MySQLi</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="page-header text-right">Vue.js Registration with PHP/MySQLi</h1>
<div id="register">
<div class="col-xl-4">

<div class="live panel panel-success">
<div class="live panel-heading"><span class="live glyphicon glyphicon-client"></span> Client Registration</div>
<div class="live panel-body">
<label>Client Email:</label>
<input type="text" class="live form-control" ref="email" v-model="dtlRegInfo.email" v-on:keyup="clientKeycheck">
<div class="text-right" v-if="emailErr">
<span style="font-size:13px;">{{ emailErr }}</span>
</div>
<label>client Password:</label>
<input type="password" class="form-control" ref="password" v-model="dtlRegInfo.password" v-on:keyup="clientKeycheck">
<div class="text-right" v-if="passErr">
<span style="font-size:13px;">{{ passErr }}</span>
</div>
</div>
<div class="live panel-footer">
<button class="live btn btn-default btn-block" @click="clientReg();"><span class="live glyphicon glyphicon-check"></span>Registration</button>
</div>
</div>

<div class="alert alert-danger text-right" v-if="msgError">
<button type="button" class="close" @click="msgCls();"><span aria-hidden="true">×</span></button>
<span class="live glyphicon glyphicon-alert"></span> {{ msgError }}
</div>

<div class="alert alert-success text-right" v-if="msgofSuccess">
<button type="button" class="close" @click="msgCls();"><span aria-hidden="true">×</span></button>
<span class="live glyphicon glyphicon-check"></span> {{ msgofSuccess }}
</div>

</div>

<div class="col-xl-8">
<h3>Clients Table</h3>
<table class="table">
<thead>
<th>Client ID</th>
<th>client Email</th>
<th>client Password</th>
</thead>
<tbody>
<tr v-for="client in Clients">
<td>{{ client.clientId }}</td>
<td>{{ client.email }}</td>
<td>{{ client.password }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script src="dir/vue.js"></script>
<script src="dir/axios.js"></script>
<script src="dir/main.js"></script>
</body>
</html>

main.js

var main = new Vue({
el: '#register',
data:{
msgofSuccess: "",
msgError: "",
emailErr: "",
passErr: "",
Clients: [],
dtlRegInfo: {email: '', password: ''},
},

mounted: function(){
this.getAllClients();
},

methods:{
getAllClients: function(){
axios.get('api.php')
.then(function(dataRes){
if(dataRes.data.error){
main.msgError = dataRes.data.message;
}
else{
main.Clients = dataRes.data.Clients;
}
});
},

clientReg: function(){
var regForm = main.toFormData(main.dtlRegInfo);
axios.post('api.php?do_act=register', regForm)
.then(function(dataRes){
console.log(dataRes);
if(dataRes.data.email){
main.emailErr = dataRes.data.message;
main.emailFocus();
main.msgCls();
}
else if(dataRes.data.password){
main.passErr = dataRes.data.message;
main.emailErr='';
main.passFocus();
main.msgCls();
}
else if(dataRes.data.error){
main.msgError = dataRes.data.message;
main.emailErr='';
main.passErr='';
}
else{
main.msgofSuccess = dataRes.data.message;
main.dtlRegInfo = {email: '', password:''};
main.emailErr='';
main.passErr='';
main.getAllClients();
}
});
},

emailFocus: function(){
this.$refs.email.focus();
},

passFocus: function(){
this.$refs.password.focus();
},

clientKeycheck: function(event) {
if(event.key == "Enter"){
main.clientReg();
}
},

toFormData: function(obj){
var liveFormData = new FormData();
for(var key in obj){
liveFormData.append(key, obj[key]);
}
return liveFormData;
},

msgCls: function(){
main.msgError = '';
main.msgofSuccess = '';
}

}
});

api.php

<?php

$db_con = new mysqli("localhost", "username", "Your Password", "DataBase Name");

if ($db_con->connect_error) {
die("DataBase Connection failed: " . $db_con->connect_error);
}

$res_output = array('error' => false, 'email'=> false, 'password' => false);

$do_act = 'read';

if(isset($_GET['do_act'])){
$do_act = $_GET['do_act'];
}


if($do_act == 'read'){
$sql = "select * from Clients";
$query = $db_con->query($sql);
$Clients = array();

while($row = $query->fetch_array()){
array_push($Clients, $row);
}

$res_output['Clients'] = $Clients;
}

if($do_act == 'register'){

function checkStr($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

$email = checkStr($_POST['email']);
$password = checkStr($_POST['password']);

if($email==''){
$res_output['email'] = true;
$res_output['message'] = "Client Email is required";
}

elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$res_output['email'] = true;
$res_output['message'] = "Invalid Client Email Format";
}

elseif($password==''){
$res_output['password'] = true;
$res_output['message'] = "Client Password is required";
}

else{
$sql="select * from Clients where email='$email'";
$query=$db_con->query($sql);

if($query->num_rows > 0){
$res_output['email'] = true;
$res_output['message'] = "client Email already exist";
}

else{
$sql = "insert into Clients (email, password) values ('$email', '$password')";
$query = $db_con->query($sql);

if($query){
$res_output['message'] = "Client Added Successfully";
}
else{
$res_output['error'] = true;
$res_output['message'] = "Could not add Client";
}
}
}
}

$db_con->close();
//simple pass a json format data in php
header("Content-type: application/json");
echo json_encode($res_output);
die();
?>

I hope you have Got What is Vue.js User Registration/Sign up with PHP/MySQLi And how it works.


#vue #vuejs #php

Vue.js User Authentication with PHP
1.00 GEEK