From b99e8dadd0a1cc1ae95c79528549be71f65b65d7 Mon Sep 17 00:00:00 2001 From: Artemy Date: Thu, 11 Aug 2022 16:00:30 +0300 Subject: [PATCH] feat: check auth --- index.html | 1 - package-lock.json | 15 +++ package.json | 1 + src-tauri/Cargo.lock | 211 ++++++++++++++++++++++++++++++++++++++ src-tauri/Cargo.toml | 5 + src-tauri/src/commands.rs | 22 ++++ src-tauri/src/main.rs | 15 ++- src-tauri/src/types.rs | 6 ++ src/App.jsx | 47 +++++---- src/components/button.jsx | 15 ++- src/components/menu.jsx | 39 ++++++- src/index.css | 2 +- src/pages/checkAuth.jsx | 20 ++++ 13 files changed, 365 insertions(+), 34 deletions(-) create mode 100644 src-tauri/src/commands.rs create mode 100644 src-tauri/src/types.rs create mode 100644 src/pages/checkAuth.jsx diff --git a/index.html b/index.html index 4ce0e4e..8b9c1e4 100644 --- a/index.html +++ b/index.html @@ -3,7 +3,6 @@ - OSMA app diff --git a/package-lock.json b/package-lock.json index e003945..aed344f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 93338b3..237f27c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index e6fed88..579e8b6 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -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" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 2f1dbb2..9f6c1d8 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -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 diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs new file mode 100644 index 0000000..2b607dd --- /dev/null +++ b/src-tauri/src/commands.rs @@ -0,0 +1,22 @@ +use crate::AppData; +use reqwest::blocking::*; +use tauri::{App, State}; + +#[tauri::command] +pub fn check_auth(state: State) -> 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, + } + } +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 860b10d..f982343 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -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::(&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"); } diff --git a/src-tauri/src/types.rs b/src-tauri/src/types.rs new file mode 100644 index 0000000..a4ede8c --- /dev/null +++ b/src-tauri/src/types.rs @@ -0,0 +1,6 @@ +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct AppData { + pub jwt: String, +} diff --git a/src/App.jsx b/src/App.jsx index f3ec408..5061083 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,34 +1,33 @@ -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 ( -
+
- - - Page for check authorisation -
- } - /> - Main page!
} - /> - Your library!} - /> - Your downloads!} - /> - +
+ + } /> + Main page!
} + /> + Your library!} + /> + Your downloads!} + /> + Options page} + /> + + ); } diff --git a/src/components/button.jsx b/src/components/button.jsx index 7f7f0e5..8e15d7a 100644 --- a/src/components/button.jsx +++ b/src/components/button.jsx @@ -1,11 +1,20 @@ function Button(props) { return ( - -
+ +
{props.children}
); } -export default Button; +function IconWithButton(props) { + return ( +
+ {props.icon} +
{props.children}
+
+ ); +} + +export { Button, IconWithButton }; diff --git a/src/components/menu.jsx b/src/components/menu.jsx index 553765f..60a5aa5 100644 --- a/src/components/menu.jsx +++ b/src/components/menu.jsx @@ -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 (
- - - + + + +
); } diff --git a/src/index.css b/src/index.css index b098903..2c9fe39 100644 --- a/src/index.css +++ b/src/index.css @@ -4,5 +4,5 @@ body{ @apply dark:bg-zinc-900; - @apply bg-zinc-300; + @apply bg-white; } \ No newline at end of file diff --git a/src/pages/checkAuth.jsx b/src/pages/checkAuth.jsx new file mode 100644 index 0000000..f79e819 --- /dev/null +++ b/src/pages/checkAuth.jsx @@ -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
Wait...
; +} + +export default CheckAuth;