Compare commits

...

29 Commits

Author SHA1 Message Date
b66d5a8f5f 49 2024-07-04 21:19:34 +05:30
f67317a49a 48 2024-07-04 14:48:28 +05:30
121dba686f 47 2024-07-04 14:38:00 +05:30
e2821fbf58 46 2024-07-04 04:33:09 +05:30
c4b725b4a6 45 2024-07-04 04:31:15 +05:30
be276acef4 44 2024-07-04 04:27:02 +05:30
0b86e17ee3 43 2024-07-04 03:49:03 +05:30
e0ef61d643 42 2024-07-04 03:48:00 +05:30
5213a0da3c 41 2024-07-04 03:35:06 +05:30
ad98c91951 40 2024-07-04 03:34:10 +05:30
4cf041a11d 39 2024-07-04 03:33:09 +05:30
6258cf9e02 38 2024-07-02 15:34:44 +05:30
8f2c51ac5c 37 2024-07-02 15:34:12 +05:30
79c858a7c7 36 2024-07-02 04:15:05 +05:30
a24a00080d 35 2024-07-02 04:10:08 +05:30
246fc700e4 34 2024-07-02 04:03:12 +05:30
9d583c0dc9 33 2024-07-02 03:48:58 +05:30
e600bdd4f2 32 2024-07-02 03:48:40 +05:30
d49fcf19ea 31 2024-07-01 23:03:06 +05:30
11a81f098f 30 2024-07-01 02:43:57 +05:30
6f5fae7d74 29 2024-07-01 02:43:38 +05:30
a7ac1dc642 28 2024-06-30 02:38:53 +05:30
e429ff701a 27 2024-06-30 02:38:38 +05:30
0ba46b6eca 26 2024-06-30 01:27:30 +05:30
c3d2ed9a7a 25 2024-06-30 01:25:29 +05:30
60f1170234 24 2024-06-30 01:24:35 +05:30
ee3e4c1fc2 23 2024-06-30 01:24:12 +05:30
ff279dbcf6 22 2024-06-29 22:35:26 +05:30
17027b3033 21 2024-06-29 22:34:40 +05:30
9 changed files with 380 additions and 17 deletions

70
docs/api.md Normal file
View 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
View 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.

View File

@@ -2,7 +2,69 @@ module main
import vweb import vweb
@[post] @['/signup'; post]
pub fn (mut app App) signup() vweb.Result{ pub fn (mut app App) post_signup(username string, email string, password string) vweb.Result{
return $vweb.html() 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)
}

View File

@@ -10,3 +10,9 @@ mut:
password string @[sql_type: 'TEXT'] password string @[sql_type: 'TEXT']
// products []Product @[fkey: 'user_id'] // products []Product @[fkey: 'user_id']
} }
pub struct Noti {
mut:
status string
message string
}

50
src/htmx.v Normal file
View 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>'
}

View File

@@ -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]
}

View File

@@ -4,18 +4,33 @@
<title>Log In</title> <title>Log In</title>
<!-- Add this line to include the Tailwind CSS library --> <!-- 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.min.css'
@js 'https://unpkg.com/htmx.org@2.0.0'
</head> </head>
<body class="bg-gray-100 flex items-center justify-center min-h-screen"> <body class="bg-gray-100 flex items-center justify-center min-h-screen">
<div class="w-full max-w-md"> <div class="w-full max-w-md">
<h1 class="text-3xl mb-6 font-bold text-center text-blue-500">Welcome Back!</h1> <h1 class="text-3xl mb-6 font-bold text-center text-blue-500">Welcome Back!</h1>
<form action="/login" method="post"> <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> <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>
<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> <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> </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> <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> </form>

View File

@@ -3,23 +3,40 @@
<head> <head>
<title>Sign Up</title> <title>Sign Up</title>
<!-- Add this line to include the Tailwind CSS library --> <!-- 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> </head>
<body class="bg-gray-100 flex items-center justify-center min-h-screen"> <body class="bg-gray-100 flex items-center justify-center min-h-screen">
<div class="w-full max-w-md"> <div class="w-full max-w-md">
<h1 class="text-3xl mb-6 font-bold text-center text-blue-500">Create an Account</h1> <h1 class="text-3xl mb-6 font-bold text-center text-blue-500">Create an Account</h1>
<form action="/signup" method="post"> <form action="/signup" method="post">
<div class="mb-4"> <div class="mb-4"
<label for="username" class="block text-sm font-medium leading-relaxed tracking-wide mb-2">Username:</label> hx-target="this"
<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-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>
<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> <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>
<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> <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> </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> <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> </form>

View File

@@ -4,4 +4,49 @@ import vweb
pub fn (mut app App) signup() vweb.Result{ pub fn (mut app App) signup() vweb.Result{
return $vweb.html() 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))
}