feat: check auth

This commit is contained in:
Artemy 2022-08-11 16:00:30 +03:00
parent 89cddd317c
commit b99e8dadd0
13 changed files with 365 additions and 34 deletions

View file

@ -3,7 +3,6 @@
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>OSMA app</title>
</head>

15
package-lock.json generated
View file

@ -8,6 +8,7 @@
"name": "osma",
"version": "0.0.0",
"dependencies": {
"@heroicons/react": "^1.0.6",
"@tauri-apps/api": "^1.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
@ -440,6 +441,14 @@
"node": ">=6.9.0"
}
},
"node_modules/@heroicons/react": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/@heroicons/react/-/react-1.0.6.tgz",
"integrity": "sha512-JJCXydOFWMDpCP4q13iEplA503MQO3xLoZiKum+955ZCtHINWnx26CUxVxxFQu/uLb4LW3ge15ZpzIkXKkJ8oQ==",
"peerDependencies": {
"react": ">= 16"
}
},
"node_modules/@jridgewell/gen-mapping": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.1.1.tgz",
@ -2655,6 +2664,12 @@
"to-fast-properties": "^2.0.0"
}
},
"@heroicons/react": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/@heroicons/react/-/react-1.0.6.tgz",
"integrity": "sha512-JJCXydOFWMDpCP4q13iEplA503MQO3xLoZiKum+955ZCtHINWnx26CUxVxxFQu/uLb4LW3ge15ZpzIkXKkJ8oQ==",
"requires": {}
},
"@jridgewell/gen-mapping": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.1.1.tgz",

View file

@ -10,6 +10,7 @@
"tauri": "tauri"
},
"dependencies": {
"@heroicons/react": "^1.0.6",
"@tauri-apps/api": "^1.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",

211
src-tauri/Cargo.lock generated
View file

@ -57,6 +57,8 @@ checksum = "bb07d2053ccdbe10e2af2995a2f116c1330396493dc1269f6a91d0ae82e19704"
name = "app"
version = "0.1.0"
dependencies = [
"openssl",
"reqwest",
"serde",
"serde_json",
"tauri",
@ -607,6 +609,15 @@ version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ef6b89e5b37196644d8796de5268852ff179b44e96276cf4290264843743bb7"
[[package]]
name = "encoding_rs"
version = "0.8.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b"
dependencies = [
"cfg-if",
]
[[package]]
name = "fastrand"
version = "1.8.0"
@ -1079,6 +1090,25 @@ dependencies = [
"syn",
]
[[package]]
name = "h2"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37a82c6d637fc9515a4694bbf1cb2457b79d81ce52b3108bdeea58b07dd34a57"
dependencies = [
"bytes",
"fnv",
"futures-core",
"futures-sink",
"futures-util",
"http",
"indexmap",
"slab",
"tokio",
"tokio-util",
"tracing",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
@ -1134,12 +1164,72 @@ dependencies = [
"itoa 1.0.2",
]
[[package]]
name = "http-body"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1"
dependencies = [
"bytes",
"http",
"pin-project-lite",
]
[[package]]
name = "http-range"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573"
[[package]]
name = "httparse"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "496ce29bb5a52785b44e0f7ca2847ae0bb839c9bd28f69acac9b99d461c0c04c"
[[package]]
name = "httpdate"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
[[package]]
name = "hyper"
version = "0.14.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02c929dc5c39e335a03c405292728118860721b10190d98c2a0f0efd5baafbac"
dependencies = [
"bytes",
"futures-channel",
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"httparse",
"httpdate",
"itoa 1.0.2",
"pin-project-lite",
"socket2",
"tokio",
"tower-service",
"tracing",
"want",
]
[[package]]
name = "hyper-tls"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
dependencies = [
"bytes",
"hyper",
"native-tls",
"tokio",
"tokio-native-tls",
]
[[package]]
name = "ico"
version = "0.1.0"
@ -1235,6 +1325,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "ipnet"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "879d54834c8c76457ef4293a689b2a8c59b076067ad77b15efafbb05f92a592b"
[[package]]
name = "itoa"
version = "0.4.8"
@ -1472,6 +1568,12 @@ dependencies = [
"autocfg",
]
[[package]]
name = "mime"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "miniz_oxide"
version = "0.5.3"
@ -1481,6 +1583,18 @@ dependencies = [
"adler",
]
[[package]]
name = "mio"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf"
dependencies = [
"libc",
"log",
"wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys",
]
[[package]]
name = "native-tls"
version = "0.2.10"
@ -2202,6 +2316,43 @@ dependencies = [
"winapi",
]
[[package]]
name = "reqwest"
version = "0.11.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b75aa69a3f06bbcc66ede33af2af253c6f7a86b1ca0033f60c580a27074fbf92"
dependencies = [
"base64",
"bytes",
"encoding_rs",
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"hyper",
"hyper-tls",
"ipnet",
"js-sys",
"lazy_static",
"log",
"mime",
"native-tls",
"percent-encoding",
"pin-project-lite",
"serde",
"serde_json",
"serde_urlencoded",
"tokio",
"tokio-native-tls",
"tower-service",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"winreg",
]
[[package]]
name = "rfd"
version = "0.9.1"
@ -2523,6 +2674,16 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1"
[[package]]
name = "socket2"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "soup2"
version = "0.2.1"
@ -2983,10 +3144,38 @@ checksum = "57aec3cfa4c296db7255446efb4928a6be304b431a806216105542a67b6ca82e"
dependencies = [
"autocfg",
"bytes",
"libc",
"memchr",
"mio",
"num_cpus",
"once_cell",
"pin-project-lite",
"socket2",
"winapi",
]
[[package]]
name = "tokio-native-tls"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b"
dependencies = [
"native-tls",
"tokio",
]
[[package]]
name = "tokio-util"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc463cd8deddc3770d20f9852143d50bf6094e640b485cb2e189a2099085ff45"
dependencies = [
"bytes",
"futures-core",
"futures-sink",
"pin-project-lite",
"tokio",
"tracing",
]
[[package]]
@ -2998,6 +3187,12 @@ dependencies = [
"serde",
]
[[package]]
name = "tower-service"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52"
[[package]]
name = "tracing"
version = "0.1.35"
@ -3069,6 +3264,12 @@ dependencies = [
"serde_json",
]
[[package]]
name = "try-lock"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]]
name = "typenum"
version = "1.15.0"
@ -3209,6 +3410,16 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "want"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0"
dependencies = [
"log",
"try-lock",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"

View file

@ -19,6 +19,11 @@ serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.5", features = ["api-all"] }
reqwest = { version = "0.11", features = ["blocking", "json"] }
[target.'cfg(target_os = "linux")'.dependencies]
openssl = "0.10.41"
[features]
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL

22
src-tauri/src/commands.rs Normal file
View file

@ -0,0 +1,22 @@
use crate::AppData;
use reqwest::blocking::*;
use tauri::{App, State};
#[tauri::command]
pub fn check_auth(state: State<AppData>) -> bool {
println!("Checking auth");
if state.jwt == "null" {
return false;
} else {
let client = Client::new();
let response = client
.get("https://osma-server.herokuapp.com/personal_library")
.bearer_auth(&state.jwt)
.send();
match response {
Ok(_) => true,
Err(_) => false,
}
}
}

View file

@ -3,8 +3,21 @@
windows_subsystem = "windows"
)]
use std::fs;
mod types;
use types::*;
mod commands;
fn main() {
let options = fs::read_to_string("./options.json").unwrap_or("{\"jwt\":null}".to_string());
tauri::Builder::default()
.manage(
serde_json::from_str::<AppData>(&options).unwrap_or(AppData {
jwt: "null".to_string(),
}),
)
.invoke_handler(tauri::generate_handler![commands::check_auth])
.run(tauri::generate_context!())
.expect("error while running tauri application");
.expect("error while running osma app");
}

6
src-tauri/src/types.rs Normal file
View file

@ -0,0 +1,6 @@
use serde::Deserialize;
#[derive(Deserialize)]
pub struct AppData {
pub jwt: String,
}

View file

@ -1,35 +1,34 @@
import { useState } from "react";
import "./components/menu";
import Menu from "./components/menu";
import { Routes, Route } from "react-router-dom";
import CheckAuth from "./pages/checkAuth";
function App() {
return (
<div className="grid grid-cols-1 lg:grid-cols-5 gap-10 text-white">
<div className="grid grid-cols-1 lg:grid-cols-5 gap-10 text-black dark:text-white">
<Menu />
<div className="col-span-4 p-5 m-4 rounded-2xl">
<Routes>
<Route
path="/"
element={
<div className="text-white col-span-4">
Page for check authorisation
</div>
}
/>
<Route path="/" element={<CheckAuth />} />
<Route
path="main"
element={<div className="text-white col-span-4">Main page!</div>}
element={<div className="col-span-4">Main page!</div>}
/>
<Route
path="library"
element={<div className="text-white col-span-4">Your library!</div>}
element={<div className="col-span-4">Your library!</div>}
/>
<Route
path="downloads"
element={<div className="text-white col-span-4">Your downloads!</div>}
element={<div className="col-span-4">Your downloads!</div>}
/>
<Route
path="options"
element={<div className="col-span-4">Options page</div>}
/>
</Routes>
</div>
</div>
);
}

View file

@ -1,11 +1,20 @@
function Button(props) {
return (
<a href={props.href}>
<div className="transition-transform w-48 ease-[cubic-bezier(.69,.58,.32,1.69)] delay-60 hover:scale-105 p-2 pl-6 text-lg bg-zinc-400 hover:bg-zinc-600 dark:bg-zinc-600 dark:hover:bg-zinc-800 rounded-2xl">
<a href={props.href} className={props.className}>
<div className="transition-transform w-48 ease-[cubic-bezier(.69,.58,.32,1.69)] delay-60 hover:scale-105 p-2 pl-6 text-lg bg-zinc-100 hover:bg-zinc-300 dark:bg-zinc-600 dark:hover:bg-zinc-800 rounded-2xl">
{props.children}
</div>
</a>
);
}
export default Button;
function IconWithButton(props) {
return (
<div className="grid grid-cols-4">
{props.icon}
<div className="grid-span-3">{props.children}</div>
</div>
);
}
export { Button, IconWithButton };

View file

@ -1,11 +1,42 @@
import Button from "./button";
import { Button, IconWithButton } from "./button";
import {
MenuIcon,
ArchiveIcon,
ArrowCircleDownIcon,
CogIcon,
} from "@heroicons/react/outline";
function Menu() {
return (
<div className="grid grid-cols-1 gap-3 m-4">
<Button href="/main">Main</Button>
<Button href="/library">Library</Button>
<Button href="/downloads">Downloads</Button>
<Button href="/main">
<IconWithButton icon={<MenuIcon className="h-7 w-7" />}>
Main
</IconWithButton>
</Button>
<Button href="/library">
<IconWithButton
icon={<ArchiveIcon className="transform translate-z-0 h-7 w-7" />}
>
Library
</IconWithButton>
</Button>
<Button href="/downloads">
<IconWithButton
icon={
<ArrowCircleDownIcon className="transform translate-z-0 h-7 w-7" />
}
>
Downloads
</IconWithButton>
</Button>
<Button href="/options">
<IconWithButton
icon={<CogIcon className="transform translate-z-0 h-7 w-7" />}
>
Options
</IconWithButton>
</Button>
</div>
);
}

View file

@ -4,5 +4,5 @@
body{
@apply dark:bg-zinc-900;
@apply bg-zinc-300;
@apply bg-white;
}

20
src/pages/checkAuth.jsx Normal file
View file

@ -0,0 +1,20 @@
import { useNavigate } from "react-router-dom";
import { invoke } from "@tauri-apps/api/tauri";
import { useEffect } from "react";
import { Button } from "../components/button";
import React, { useState } from "react";
function CheckAuth() {
const navigate = useNavigate();
useEffect(() => {
invoke("check_auth").then((authorised) => {
if (authorised) navigate("/main");
else navigate("/auth");
});
});
return <div>Wait...</div>;
}
export default CheckAuth;