Skip to content

Commit

Permalink
Release: v1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
XPH0816 committed Jan 13, 2025
1 parent 91e3234 commit 0d859d7
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 25 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:
args: ''

runs-on: ${{ matrix.settings.platform }}
env:
DB_USER: ${{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -71,6 +74,32 @@ jobs:
# If you don't have `beforeBuildCommand` configured you may want to build your frontend here too.
run: pnpm install # change this to npm or pnpm depending on which one you use.

- name: replace .env file (macos and linux only)
if: matrix.settings.platform != 'windows-latest'
run: |
if [[ -n "${{ secrets.DB_USER }}" ]] && [[ -n "${{ secrets.DB_PASSWORD }}" ]]; then
sed -i 's/DATABASE_URL=mysql:\/\/root:@127.0.0.1:3306\/libraryroom/DATABASE_URL=mysql:\/\/${{ secrets.DB_USER }}:${{ secrets.DB_PASSWORD }}@127.0.0.1:3306\/libraryroom/g' "src-tauri/.env"
elif [[ -n "${{ secrets.DB_USER }}" ]]; then
sed -i 's/DATABASE_URL=mysql:\/\/root:@127.0.0.1:3306\/libraryroom/DATABASE_URL=mysql:\/\/${{ secrets.DB_USER }}:@127.0.0.1:3306\/libraryroom/g' "src-tauri/.env"
elif [[ -n "${{ secrets.DB_PASSWORD }}" ]]; then
sed -i 's/DATABASE_URL=mysql:\/\/root:@127.0.0.1:3306\/libraryroom/DATABASE_URL=mysql:\/\/root:${{ secrets.DB_PASSWORD }}@127.0.0.1:3306\/libraryroom/g' "src-tauri/.env"
fi
- name: replace .env file (windows only)
if: matrix.settings.platform == 'windows-latest'
run: |
$envContent = Get-Content src-tauri/.env
if ($env:DB_USER) {
if ($env:DB_PASSWORD) {
$envContent = $envContent -replace 'DATABASE_URL=mysql://root:@127.0.0.1:3306/libraryroom', "DATABASE_URL=mysql://$env:DB_USER:$env:[email protected]:3306/libraryroom"
} else {
$envContent = $envContent -replace 'DATABASE_URL=mysql://root:@127.0.0.1:3306/libraryroom', "DATABASE_URL=mysql://$env:DB_USER:@127.0.0.1:3306/libraryroom"
}
} elseif ($env:DB_PASSWORD) {
$envContent = $envContent -replace 'DATABASE_URL=mysql://root:@127.0.0.1:3306/libraryroom', "DATABASE_URL=mysql://root:$env:[email protected]:3306/libraryroom"
}
$envContent | Set-Content src-tauri/.env
- uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "libraryroombookingsystem",
"private": true,
"version": "1.0.5",
"version": "1.0.6",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
107 changes: 106 additions & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "LibraryRoomBookingSystem"
version = "1.0.5"
version = "1.0.6"
description = "A Library Room Booking System built with Tauri and Rust"
authors = ["Lim Shi Song [email protected]"]
edition = "2021"
Expand All @@ -9,6 +9,7 @@ edition = "2021"

[build-dependencies]
tauri-build = { version = "1", features = [] }
dotenvy = "0.15.7"

[dependencies]
tauri = { version = "1", features = ["shell-open"] }
Expand All @@ -17,6 +18,7 @@ jsonwebtoken = "9"
serde_json = "1"
dotenvy = "0.15.7"
chrono = "0.4.38"
envcrypt = "0.5.0"

[dependencies.tauri-plugin-sql]
git = "https://github.com/tauri-apps/plugins-workspace"
Expand Down
8 changes: 7 additions & 1 deletion src-tauri/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
fn main() {
tauri_build::build()
println!("cargo:rerun-if-changed=.env");

for item in dotenvy::dotenv_iter().unwrap() {
let (key, value) = item.unwrap();
println!("cargo:rustc-env={key}={value}");
}
tauri_build::build();
}
10 changes: 8 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

mod models;

use envcrypt::envc;
use jsonwebtoken::{
decode, encode, get_current_timestamp, Algorithm, DecodingKey, EncodingKey, Header, Validation,
};
Expand All @@ -12,7 +13,7 @@ use models::{User, UserForm, UserToken};
#[tauri::command]
fn login(user: UserForm) -> UserToken {
let iat = get_current_timestamp() as usize;
let exp = iat + 2 * 60 * 60;
let exp = iat + 5;
let user_data = User {
id: user.user_id,
username: user.username.clone(),
Expand Down Expand Up @@ -60,10 +61,15 @@ fn get_env(key: &str) -> Result<String, String> {
}
}

#[tauri::command]
fn get_db_url() -> Result<String, String> {
Ok(envc!("DATABASE_URL").to_string())
}

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_sql::Builder::default().build())
.invoke_handler(tauri::generate_handler![login, get_env, check_auth])
.invoke_handler(tauri::generate_handler![login, get_env, check_auth, get_db_url])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
5 changes: 1 addition & 4 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"package": {
"productName": "LibraryRoomBookingSystem",
"version": "1.0.5"
"version": "1.0.6"
},
"tauri": {
"allowlist": {
Expand All @@ -31,9 +31,6 @@
"active": true,
"targets": "all",
"identifier": "com.library-room-booking-system.app",
"resources": [
".env"
],
"icon": [
"icons/32x32.png",
"icons/128x128.png",
Expand Down
7 changes: 3 additions & 4 deletions src/lib/auth.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { invoke } from "@tauri-apps/api/tauri";
import { goto } from "$app/navigation";
import { hashPassword, verify } from "./crypto/hashing";
import Cookies from "js-cookie";
import { getDB } from "./db";
import { getUserByEmail } from "./models/user";
import { setItem } from "./helper";

export async function login(email, password, usertype, link) {
let user = await getUserByEmail(email, usertype);
Expand All @@ -17,9 +17,8 @@ export async function login(email, password, usertype, link) {
let token = await invoke("login", {
user,
});

let expiredDate = new Date(token.exp * 1000);
Cookies.set("token", token.token, { expires: expiredDate });
console.log(token);
setItem("token", token.token, token.exp);
goto(link);
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Database from "tauri-plugin-sql-api";
import { invoke } from "@tauri-apps/api/tauri";

export async function getDB() {
let dbUrl = await invoke("get_env", { key: "DATABASE_URL" });
let dbUrl = await invoke("get_db_url");
return await Database.load(dbUrl);
}
33 changes: 33 additions & 0 deletions src/lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,37 @@ export function isNoRoomNumber(roomName) {
return getRoomType().find(room => room.name === roomName).price === 0;
}
return false;
}

/**
* @param {String} key
* @param {String} value
* @param {Number} maxAge
*/
export function setItem(key, value, maxAge = 30 * 30 * 60) {
maxAge = maxAge * 1000;
let result = {
data: value
}

if (maxAge) {
result.expireTime = maxAge;
}
window.localStorage.setItem(key, JSON.stringify(result));
}

/**
* @param {String} key
* @returns
*/
export function getItem(key) {
let result = JSON.parse(window.localStorage.getItem(key));
if (result) {
if (result.expireTime <= Date.now()) {
window.localStorage.removeItem(key);
return null;
}
return result.data;
}
return null;
}
Loading

0 comments on commit 0d859d7

Please sign in to comment.