Skip to content

Commit

Permalink
save into .env
Browse files Browse the repository at this point in the history
  • Loading branch information
divear committed Aug 17, 2024
1 parent 990e0bc commit f5688ce
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 11 deletions.
53 changes: 52 additions & 1 deletion app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
@tailwind components;
@tailwind utilities;

:root {
--bg: #0a647c;
}
body {
color: white !important;
margin: 0;
Expand Down Expand Up @@ -48,7 +51,7 @@ input {
}
button {
cursor: pointer;
background-color: #0a647c;
background-color: var(--bg);
border: none;
font-size: 2rem;
border-radius: 0.5vw;
Expand Down Expand Up @@ -122,3 +125,51 @@ button {
display: flex;
background: white;
}

.custom-checkbox {
margin: 0;
display: flex;
align-items: center;
cursor: pointer;
font-size: 1rem;
text-align: center;
}

.custom-checkbox input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}

.checkmark {
position: relative;
height: 20px;
width: 20px;
background-color: #ccc;
border-radius: 4px;
margin-right: 8px;
transition: background-color 0.3s;
}

.custom-checkbox input:checked ~ .checkmark {
background-color: var(--bg);
}

.checkmark:after {
content: "";
position: absolute;
display: none;
left: 7px;
top: 3px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
}

.custom-checkbox input:checked ~ .checkmark:after {
display: block;
}
25 changes: 21 additions & 4 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// import Image from "next/image";
import { invoke } from "@tauri-apps/api/tauri";
// import logo from "../public/vercel.svg";
import { useState } from "react";
import { useState, useEffect } from "react";

export default function Home() {
const [error, setError] = useState("");
Expand All @@ -18,14 +18,24 @@ export default function Home() {
const [chosenShowGrades, setChosenShowGrades] = useState<number[]>();
const [chosenValues, setChosenValues] = useState<number[]>();
const [showPass, setShowPass] = useState(false);
const [isSave, setIsSave] = useState(false);

console.log();
useEffect(() => {
if (process.env.NEXT_PUBLIC_USERNAME && process.env.NEXT_PUBLIC_PASSWORD) {
setUsername(process.env.NEXT_PUBLIC_USERNAME);
setPassword(process.env.NEXT_PUBLIC_PASSWORD);
setIsSave(true);
}
}, []);

function signin(e: any) {
e.preventDefault();
if (!username || !password) {
setError("You have to fill all fields!");
return;
}
invoke<string>("subjects", { username, password })
invoke<string>("subjects", { username, password, isSave })
.then((result) => {
console.log(result);
if (result[0] == "could not get subjects") {
Expand Down Expand Up @@ -120,10 +130,17 @@ export default function Home() {
{showPass ? "👁" : "🔒"}
</button>
</div>
<label>
<input type="checkbox"></input>

<label className="custom-checkbox">
<input
checked={isSave}
onClick={() => setIsSave(!isSave)}
type="checkbox"
/>
<span className="checkmark"></span>
Save login for later
</label>

<br />
<button className="signButton">Sign in</button>

Expand Down
28 changes: 24 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use colored::Colorize;
use headless_chrome::Browser;
use std::{collections::HashSet, env, error::Error, fmt, time::Duration};
use std::{
collections::HashSet, env, error::Error, fmt, fs, fs::File, io::prelude::*, time::Duration,
};

mod term;
const WAIT_LIMIT: u64 = 15;
struct ZnamkaStruct {
Expand Down Expand Up @@ -45,8 +48,8 @@ fn process_percent(znamka: &str) -> Option<f32> {
}

#[tauri::command]
fn subjects(username: &str, password: &str) -> (String, String) {
match get_subjects(username, password) {
fn subjects(username: &str, password: &str, is_save: bool) -> (String, String) {
match get_subjects(username, password, is_save) {
Ok((subjects, grades)) => {
println!("success");
(subjects, grades)
Expand All @@ -60,10 +63,27 @@ fn subjects(username: &str, password: &str) -> (String, String) {
}
}
}
fn get_subjects(username: &str, password: &str) -> Result<(String, String), Box<dyn Error>> {
fn get_subjects(
username: &str,
password: &str,
is_save: bool,
) -> Result<(String, String), Box<dyn Error>> {
let browser = Browser::default()?;
let tab = browser.new_tab()?;

if is_save {
let mut file = File::create("../.env")?;
file.write_all(
format!("NEXT_PUBLIC_USERNAME={username}\nNEXT_PUBLIC_PASSWORD={password}\n")
.as_bytes(),
)
.expect("not written");
println!("created .env");
} else {
fs::remove_file("../.env")?;
println!("deleted .env");
}

loop {
tab.navigate_to("https://sspbrno.edupage.org/login/edubarLogin.php")?;
tab.wait_for_element("input#home_Login_2e1")?.click()?;
Expand Down
6 changes: 4 additions & 2 deletions src-tauri/src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ pub fn term() -> Result<(), Box<dyn Error>> {
match dotenv() {
Ok(..) => {
println!(".env found, signing in...");
username = env::var("USERNAME").expect("USERNAME environment variable not set");
password = env::var("PASSWORD").expect("PASSWORD environment variable not set");
username =
env::var("NEXT_PUBLIC_USERNAME").expect("USERNAME environment variable not set");
password =
env::var("NEXT_PUBLIC_PASSWORD").expect("PASSWORD environment variable not set");
}
Err(..) => {
println!(".env not found, sign in manually: ");
Expand Down
1 change: 1 addition & 0 deletions todo.todo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [x] the grades
- [ ] loading page
- [ ] add the option to "save login"
- [ ] make the checkbox visible
- [ ] sign off button
- [x] add the option to "show password"
- [x] make the password be type="password"
Expand Down

0 comments on commit f5688ce

Please sign in to comment.