Giter Site home page Giter Site logo

simple-login-vue's Introduction

simple-login-vue

This is a simple Login Vue form.

This will consume the Laravel Endpoints

https://github.com/jdevstatic/laravel-test-crud

Setup On Windows

Download Node.js and select npm.

Install Vue CLI too,

npm install -g @vue/cli`

and axios

npm install axios --save

Running Project

You can download this as a zip file, then extract. Then open cmd and change directory to the target project

then

npm install

then

npm run serve

Tutorial

The index.js

The index.js inside the router folder will route the pages.

as you can see from the code below, there are the paths to that

	{
		path: '/login',
		name: 'Login',
		component: function() {
			return import( /* webpackChunkName: "about" */ '../views/Login.vue')
		}
	}

now here is the one I've made to actually route them in different pages, for example, logging as admin or regular user

	{
		path: '/admin',
		name: '200',
		component: function() {
			return import( /* webpackChunkName: "about" */ '../views/Admin.vue')
		}

as you can see the name 200, which is the state returned from POST response coming from the endpoint of

http://localhost:8000/api/authenticate

this is the complete code,

import Vue from 'vue'
import VueRouter from 'vue-router'
//import Home from '../views/Home.vue'
import LoginComponent from "../views/Login.vue"
import SecureComponent from "../views/User.vue"
//import 200Component from "../views/200.vue"

Vue.use(VueRouter)

const routes = [{
		path: '/',
		redirect: {
			name: "Login"
		}
	},
	{
		path: '/about',
		name: 'About',
		// route level code-splitting
		// this generates a separate chunk (about.[hash].js) for this route
		// which is lazy-loaded when the route is visited.
		//component: function () {
		//return import(/* webpackChunkName: "about" */ '../views/About.vue')
		//}
	},
	{
		path: '/user',
		name: '201',
		component: function() {
			return import( /* webpackChunkName: "about" */ '../views/User.vue')
		}
	},
	{
		path: '/admin',
		name: '200',
		component: function() {
			return import( /* webpackChunkName: "about" */ '../views/Admin.vue')
		}
	},

	{
		path: '/error',
		name: 'Error',
		component: function() {
			return import( /* webpackChunkName: "about" */ '../views/Error.vue')
		}
	},

	{
		path: '/login',
		name: 'Login',
		component: function() {
			return import( /* webpackChunkName: "about" */ '../views/Login.vue')
		}
	}
]

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes
})

export default router

The views Folder

image

The views folder is what we see on the browser, to interact with in our case here, it's the login form.

The login view is the form and it will get the username and password and will be sent as POST through axios, then it should receive the response,

one of my highlight here is the response code, 200 for admin, 201 for users, and 404 for unsuccessful attempts

Login.vue

<template>
    <div id="login">
        <h1>Login</h1>
        <input type="text" name="username" v-model="input.username" placeholder="Username" />
        <input type="password" name="password" v-model="input.password" placeholder="Password" />
        <button type="button" v-on:click="login()">Login</button>
    </div>
</template>

<script>
import axios from "axios";

export default {
	name: 'Login',
	data() {
		return {
			ip: "",
			input: {
				username: "",
				password: ""
			},
			response: "",
			//postBody: "",
			//errors: []
		}
	},
	methods: {
		login() {
			axios({
				method: "POST",
				"url": "http://127.0.0.1:8000/api/authenticate",
				"data": this.input,
				"headers": {
					"content-type": "application/json"
				}
			}).then(result => {
				this.response = result.data;
				console.log(result.status);
				//obj2 = JSON.stringify(result.data)
				//obj3 = JSON.parse(obj2)
				//JSON.parse(JSON.stringify(result.data))

				this.$router.replace({
					name: result.status


				});

			}, error => {
				//console.error(error);
				this.$router.replace({
					name: "Error"


				});
			});


			//axios.post(`https://apimocha.com/jdevstatic/example/3`, {
			//  body: this.postBody
			//})
			//.then(response => {})
			//.catch(e => {
			//  this.errors.push(e)
			//})

			//this.$router.push({path: "/api/cruds" });

			//window.location.href = "http://127.0.0.1:8000/api/authenticate"

			//console.log(result.data);

			//axios({
			//  method: "post",
			//  url: "https://apimocha.com/jdevstatic/example",
			//  data: this.input,
			//  headers: { "Content-Type": "multipart/form-data" },
			//})
			//.then(function (response) {
			//handle success
			//console.log(response);
			//})
			//.catch(function (response) {
			//handle error
			//console.log(response);
			//});

			//console.log(this.input);

			//if(this.input.username != "" && this.input.password != "") {
			//if(this.input.username == this.$parent.mockAccount.username && this.input.password == this.$parent.mockAccount.password) {
			//this.$emit("authenticated", true);
			//this.$router.replace({ name: "Secure" });
			//} else {
			//console.log("The username and / or password is incorrect");
			//}
			//} else {
			//console.log("A username and password must be present");
			//}

		}
	}
}
</script>

<style scoped>
    #login {
        width: 500px;
        border: 1px solid #CCCCCC;
        background-color: #FFFFFF;
        margin: auto;
        margin-top: 200px;
        padding: 20px;
    }
</style>

simple-login-vue's People

Contributors

jdevfullstack avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.