Compare commits
29 Commits
367fd27eef
...
main
Author | SHA1 | Date | |
---|---|---|---|
b66d5a8f5f | |||
f67317a49a | |||
121dba686f | |||
e2821fbf58 | |||
c4b725b4a6 | |||
be276acef4 | |||
0b86e17ee3 | |||
e0ef61d643 | |||
5213a0da3c | |||
ad98c91951 | |||
4cf041a11d | |||
6258cf9e02 | |||
8f2c51ac5c | |||
79c858a7c7 | |||
a24a00080d | |||
246fc700e4 | |||
9d583c0dc9 | |||
e600bdd4f2 | |||
d49fcf19ea | |||
11a81f098f | |||
6f5fae7d74 | |||
a7ac1dc642 | |||
e429ff701a | |||
0ba46b6eca | |||
c3d2ed9a7a | |||
60f1170234 | |||
ee3e4c1fc2 | |||
ff279dbcf6 | |||
17027b3033 |
70
docs/api.md
Normal file
70
docs/api.md
Normal file
@@ -0,0 +1,70 @@
|
||||
# API Documentation
|
||||
|
||||
## Introduction
|
||||
This document provides detailed information on how to use and implement the APIs in the V8 engine.
|
||||
|
||||
## Table of Contents
|
||||
1. [Sign Up](#sign-up)
|
||||
2. [Log In](#log-in)
|
||||
3. [API Sign Up](#api-sign-up)
|
||||
4. [API Log In](#api-log-in)
|
||||
|
||||
## Sign Up
|
||||
- Description: This API allows users to sign up for a new account through the browser.
|
||||
- Usage: To use this API, follow the steps below:
|
||||
1. Step 1: Redirect the user to the sign-up page using the `/signup` route.
|
||||
2. Step 2: Fill in the required information and submit the form.
|
||||
3. Step 3: A JSON response will be returned with the user's information.
|
||||
- Payload Example:
|
||||
```JSON
|
||||
{
|
||||
"status": "success",
|
||||
"message": "User created successfully"
|
||||
}
|
||||
```
|
||||
|
||||
## Log In
|
||||
- Description: This API allows users to log in to their account through the browser.
|
||||
- Usage: To use this API, follow these steps:
|
||||
1. Step 1: Redirect the user to the log-in page using the `/login` route.
|
||||
2. Step 2: Enter the user's credentials and submit the form.
|
||||
3. Step 3: A JSON response will be returned with the user's information.
|
||||
- Payload Example:
|
||||
```JSON
|
||||
{
|
||||
"id": 123,
|
||||
"username": "user123",
|
||||
"email": "rr@ttt.yy",
|
||||
"access_level": 1,
|
||||
"password": "password123"
|
||||
}
|
||||
```
|
||||
|
||||
## API Sign Up
|
||||
- Description: This API allows users to sign up for a new account programmatically.
|
||||
- Usage: To use this API, follow these steps:
|
||||
1. Step 1: API call to the `/api/signup?username={newUser}&email={Email}&password={Password}` endpoint with the required parameters.
|
||||
2. Step 2: The server will create a new user account and return a JSON response.
|
||||
- Payload Example:
|
||||
```JSON
|
||||
{
|
||||
"status": "success",
|
||||
"message": "User created"
|
||||
}
|
||||
```
|
||||
|
||||
## API Log In
|
||||
- Description: This API allows users to log in to their account programmatically.
|
||||
- Usage: To use this API, follow these steps:
|
||||
1. Step 1: API call to the `/api/login?username={user}&password={password}` endpoint with the required parameters.
|
||||
2. Step 3: The server will authenticate the user and return a JSON response.
|
||||
- Payload Example:
|
||||
```JSON
|
||||
{
|
||||
"id": 123,
|
||||
"username": "user123",
|
||||
"email": "rr@ttt.yy",
|
||||
"access_level": 1,
|
||||
"password": "password123"
|
||||
}
|
||||
```
|
37
readme.md
Normal file
37
readme.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Authentication API
|
||||
|
||||
This is a README file for the Authentication API project.
|
||||
|
||||
## Overview
|
||||
|
||||
The Authentication API is a secure and reliable solution for user authentication in web applications. It provides endpoints for user registration, login, and password reset.
|
||||
|
||||
## Features
|
||||
|
||||
- User registration: Allows users to create new accounts by providing their email address and password.
|
||||
- User login: Allows users to authenticate themselves by providing their credentials.
|
||||
- Password reset: Provides a mechanism for users to reset their passwords if they forget them.
|
||||
|
||||
## Installation
|
||||
|
||||
To install and run the Authentication API, follow these steps:
|
||||
|
||||
1. Clone the repository: `https://somegit.dev/libexi01/5.git`
|
||||
2. Install dependencies: `v install`
|
||||
3. Start the server: `v run src`
|
||||
|
||||
## Usage
|
||||
|
||||
Once the server is running, you can use the following endpoints:
|
||||
|
||||
- `POST /api/register`: Register a new user.
|
||||
- `POST /api/login`: Authenticate a user.
|
||||
- `POST /api/reset-password`: Reset a user's password.
|
||||
- `GET /signup`: Register a new user through the Web UI.
|
||||
- `GET /login`: Authenticate a new user through the Web UI.
|
||||
|
||||
For detailed API documentation, refer to the [API documentation](/libexi01/5/src/branch/main/docs/api.md).
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! If you would like to contribute to the Authentication API project, please follow the guidelines outlined in the [CONTRIBUTING.md](/docs/CONTRIBUTING.md) file.
|
@@ -2,7 +2,69 @@ module main
|
||||
|
||||
import vweb
|
||||
|
||||
@[post]
|
||||
pub fn (mut app App) signup() vweb.Result{
|
||||
return $vweb.html()
|
||||
}
|
||||
@['/signup'; post]
|
||||
pub fn (mut app App) post_signup(username string, email string, password string) vweb.Result{
|
||||
app.service_add_user(username, email, password) or {
|
||||
mut noti := Noti{
|
||||
status: "failure",
|
||||
message: err.str()
|
||||
}
|
||||
return app.json(noti)
|
||||
}
|
||||
|
||||
noti := Noti{
|
||||
status: "success",
|
||||
message: "User created successfully"
|
||||
}
|
||||
|
||||
return app.json(noti)
|
||||
}
|
||||
|
||||
@['/controller/register']
|
||||
pub fn (mut app App) controller_signup() vweb.Result{
|
||||
username := app.query["username"]
|
||||
email := app.query["email"]
|
||||
password := app.query["password"]
|
||||
app.service_add_user(username, email, password) or {
|
||||
mut noti := Noti{
|
||||
status: "failure",
|
||||
message: err.str()
|
||||
}
|
||||
return app.json(noti)
|
||||
}
|
||||
|
||||
noti := Noti{
|
||||
status: "success",
|
||||
message: "User created successfully"
|
||||
}
|
||||
|
||||
return app.json(noti)
|
||||
}
|
||||
|
||||
@['/login'; post]
|
||||
pub fn (mut app App) post_login(username string, password string) vweb.Result{
|
||||
user := app.service_login(username, password) or {
|
||||
mut noti := Noti{
|
||||
status: "failure",
|
||||
message: err.str()
|
||||
}
|
||||
return app.json(noti)
|
||||
}
|
||||
|
||||
return app.json(user)
|
||||
}
|
||||
|
||||
@['/controller/login']
|
||||
pub fn (mut app App) controller_login() vweb.Result{
|
||||
username := app.query["username"]
|
||||
password := app.query["password"]
|
||||
user := app.service_login(username, password) or {
|
||||
mut noti := Noti{
|
||||
status: "failure",
|
||||
message: err.str()
|
||||
}
|
||||
return app.json(noti)
|
||||
}
|
||||
|
||||
return app.json(user)
|
||||
}
|
||||
|
@@ -10,3 +10,9 @@ mut:
|
||||
password string @[sql_type: 'TEXT']
|
||||
// products []Product @[fkey: 'user_id']
|
||||
}
|
||||
|
||||
pub struct Noti {
|
||||
mut:
|
||||
status string
|
||||
message string
|
||||
}
|
||||
|
50
src/htmx.v
Normal file
50
src/htmx.v
Normal file
@@ -0,0 +1,50 @@
|
||||
module main
|
||||
|
||||
pub struct HTMX {
|
||||
mut:
|
||||
name string
|
||||
message string
|
||||
mark string
|
||||
}
|
||||
|
||||
fn htmx_get_username(input HTMX) string {
|
||||
return '
|
||||
<div class="mb-4"
|
||||
hx-target="this"
|
||||
hx-swap="outerHTML"
|
||||
>
|
||||
<label for="username" class="block text-sm font-medium leading-relaxed tracking-wide mb-2">Username: <span class="text-red-600 dark:text-red-500 text-sm">${input.message}</span></label>
|
||||
<input type="text" id="username" name="username" placeholder="Enter username" ${input.mark} class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"
|
||||
hx-post="/htmx/username"
|
||||
value="${input.name}"
|
||||
/>
|
||||
</div>'
|
||||
}
|
||||
|
||||
fn htmx_get_email(input HTMX) string {
|
||||
return '
|
||||
<div class="mb-4"
|
||||
hx-target="this"
|
||||
hx-swap="outerHTML"
|
||||
>
|
||||
<label for="email" class="block text-sm font-medium leading-relaxed tracking-wide mb-2">Email: <span class="text-red-600 dark:text-red-500 text-sm">${input.message}</span></label>
|
||||
<input type="email" id="email" name="email" placeholder="Enter email" ${input.mark} class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"
|
||||
hx-post="/htmx/email"
|
||||
value="${input.name}"
|
||||
/>
|
||||
</div>'
|
||||
}
|
||||
|
||||
fn htmx_get_password(input HTMX) string {
|
||||
return '
|
||||
<div class="mb-4"
|
||||
hx-target="this"
|
||||
hx-swap="outerHTML"
|
||||
>
|
||||
<label for="password" class="block text-sm font-medium leading-relaxed tracking-wide mb-2">Password: <span class="text-red-600 dark:text-red-500 text-sm">${input.message}</span></label>
|
||||
<input type="password" id="password" name="password" placeholder="Enter password" ${input.mark} class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"
|
||||
hx-post="/htmx/password"
|
||||
value="${input.name}"
|
||||
/>
|
||||
</div>'
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
module main
|
||||
|
||||
import databases
|
||||
|
||||
fn (mut app App) service_add_user(username string, email string, password string) ! {
|
||||
if username == '' || email == '' || password == '' {
|
||||
return error('Invalid input')
|
||||
}
|
||||
|
||||
mut db := databases.create_db_connection()!
|
||||
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
|
||||
print(1)
|
||||
|
||||
|
||||
results := sql db {
|
||||
select from Auth where username == username && email == email
|
||||
}!
|
||||
|
||||
if results.len > 0 {
|
||||
return error('User already exists')
|
||||
}
|
||||
|
||||
auth_model := Auth{
|
||||
username: username,
|
||||
email: email,
|
||||
password: password,
|
||||
access_level: 1,
|
||||
}
|
||||
|
||||
mut insert_error := ''
|
||||
|
||||
sql db {
|
||||
insert auth_model into Auth
|
||||
} or { insert_error = err.msg() }
|
||||
|
||||
if insert_error != '' {
|
||||
return error(insert_error)
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut app App) service_login(username string, password string) !Auth {
|
||||
mut db := databases.create_db_connection()!
|
||||
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
|
||||
results := sql db {
|
||||
select from Auth where username == username && password == password
|
||||
}!
|
||||
|
||||
if results.len == 0 {
|
||||
return error('User not found')
|
||||
}
|
||||
|
||||
return results[0]
|
||||
}
|
@@ -4,18 +4,33 @@
|
||||
<title>Log In</title>
|
||||
<!-- Add this line to include the Tailwind CSS library -->
|
||||
@css 'https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css'
|
||||
|
||||
@js 'https://unpkg.com/htmx.org@2.0.0'
|
||||
</head>
|
||||
<body class="bg-gray-100 flex items-center justify-center min-h-screen">
|
||||
<div class="w-full max-w-md">
|
||||
<h1 class="text-3xl mb-6 font-bold text-center text-blue-500">Welcome Back!</h1>
|
||||
<form action="/login" method="post">
|
||||
<div class="mb-4">
|
||||
<!-- <div class="mb-4">
|
||||
<label for="email" class="block text-sm font-medium leading-relaxed tracking-wide mb-2">Email:</label>
|
||||
<input type="email" id="email" name="email" placeholder="Enter email address" class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"/>
|
||||
<input type="email" id="email" name="email" placeholder="Enter email address" class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"/> -->
|
||||
<div class="mb-4"
|
||||
hx-target="this"
|
||||
hx-swap="outerHTML"
|
||||
>
|
||||
<label for="username" class="block text-sm font-medium leading-relaxed tracking-wide mb-2">Username:</label>
|
||||
<input type="text" id="username" name="username" placeholder="Enter username" class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"
|
||||
hx-post="/htmx/username"
|
||||
/>
|
||||
</div>
|
||||
<div class="mb-4">
|
||||
<div class="mb-4"
|
||||
hx-target="this"
|
||||
hx-swap="outerHTML"
|
||||
>
|
||||
<label for="password" class="block text-sm font-medium leading-relaxed tracking-wide mb-2">Password:</label>
|
||||
<input type="password" id="password" name="password" placeholder="Enter password" class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"/>
|
||||
<input type="password" id="password" name="password" placeholder="Enter password" class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"
|
||||
hx-post="/htmx/password"
|
||||
/>
|
||||
</div>
|
||||
<button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">Log In</button>
|
||||
</form>
|
||||
|
@@ -3,23 +3,40 @@
|
||||
<head>
|
||||
<title>Sign Up</title>
|
||||
<!-- Add this line to include the Tailwind CSS library -->
|
||||
@css 'https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css'
|
||||
@css 'https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.css'
|
||||
|
||||
@js 'https://unpkg.com/htmx.org@2.0.0'
|
||||
</head>
|
||||
<body class="bg-gray-100 flex items-center justify-center min-h-screen">
|
||||
<div class="w-full max-w-md">
|
||||
<h1 class="text-3xl mb-6 font-bold text-center text-blue-500">Create an Account</h1>
|
||||
<form action="/signup" method="post">
|
||||
<div class="mb-4">
|
||||
<label for="username" class="block text-sm font-medium leading-relaxed tracking-wide mb-2">Username:</label>
|
||||
<input type="text" id="username" name="username" placeholder="Enter username" class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"/>
|
||||
<div class="mb-4"
|
||||
hx-target="this"
|
||||
hx-swap="outerHTML"
|
||||
>
|
||||
<label for="username" class="block text-sm font-medium leading-relaxed tracking-wide mb-2">Username:</label>
|
||||
<input type="text" id="username" name="username" placeholder="Enter username" class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"
|
||||
hx-post="/htmx/username"
|
||||
/>
|
||||
</div>
|
||||
<div class="mb-4">
|
||||
<div class="mb-4"
|
||||
hx-target="this"
|
||||
hx-swap="outerHTML"
|
||||
>
|
||||
<label for="email" class="block text-sm font-medium leading-relaxed tracking-wide mb-2">Email:</label>
|
||||
<input type="email" id="email" name="email" placeholder="Enter email address" class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"/>
|
||||
<input type="email" id="email" name="email" placeholder="Enter email address" class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"
|
||||
hx-post="/htmx/email"
|
||||
/>
|
||||
</div>
|
||||
<div class="mb-4">
|
||||
<div class="mb-4"
|
||||
hx-target="this"
|
||||
hx-swap="outerHTML"
|
||||
>
|
||||
<label for="password" class="block text-sm font-medium leading-relaxed tracking-wide mb-2">Password:</label>
|
||||
<input type="password" id="password" name="password" placeholder="Enter password" class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"/>
|
||||
<input type="password" id="password" name="password" placeholder="Enter password" class="appearance-none bg-white rounded w-full py-2 px-4 leading-tight focus:outline-none focus:shadow-outline"
|
||||
hx-post="/htmx/password"
|
||||
/>
|
||||
</div>
|
||||
<button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">Submit</button>
|
||||
</form>
|
||||
|
47
src/view.v
47
src/view.v
@@ -4,4 +4,49 @@ import vweb
|
||||
|
||||
pub fn (mut app App) signup() vweb.Result{
|
||||
return $vweb.html()
|
||||
}
|
||||
}
|
||||
|
||||
@['/htmx/username'; post]
|
||||
pub fn (mut app App) htmx_username(username string) vweb.Result{
|
||||
mut htmx := HTMX{
|
||||
name: username,
|
||||
message: "",
|
||||
mark: ""
|
||||
}
|
||||
if username == '' {
|
||||
htmx.message = "username cannot be empty"
|
||||
htmx.mark = 'style="border: 2px solid #e74c3c;"'
|
||||
return app.text(htmx_get_username(htmx))
|
||||
}
|
||||
return app.text(htmx_get_username(htmx))
|
||||
}
|
||||
|
||||
@['/htmx/email'; post]
|
||||
pub fn (mut app App) htmx_email(email string) vweb.Result{
|
||||
mut htmx := HTMX{
|
||||
name: email,
|
||||
message: "",
|
||||
mark: ""
|
||||
}
|
||||
if email == '' {
|
||||
htmx.message = "email cannot be empty"
|
||||
htmx.mark = 'style="border: 2px solid #e74c3c;"'
|
||||
return app.text(htmx_get_email(htmx))
|
||||
}
|
||||
return app.text(htmx_get_email(htmx))
|
||||
}
|
||||
|
||||
@['/htmx/password'; post]
|
||||
pub fn (mut app App) htmx_password(password string) vweb.Result{
|
||||
mut htmx := HTMX{
|
||||
name: password,
|
||||
message: "",
|
||||
mark: ""
|
||||
}
|
||||
if password == '' {
|
||||
htmx.message = "password cannot be empty"
|
||||
htmx.mark = 'style="border: 2px solid #e74c3c;"'
|
||||
return app.text(htmx_get_password(htmx))
|
||||
}
|
||||
return app.text(htmx_get_password(htmx))
|
||||
}
|
||||
|
Reference in New Issue
Block a user