Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desktop apps build and actions fixed #1

Merged
merged 3 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "build"
on:
push:
branches:
- dev
pull_request:
branches:
- dev

jobs:
test-tauri:
strategy:
fail-fast: false
matrix:
platform: [macos-latest, ubuntu-20.04, windows-latest]

runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- name: setup node
uses: actions/setup-node@v3
with:
node-version: 16
- name: install Rust stable
uses: dtolnay/rust-toolchain@stable
- name: install dependencies (ubuntu only)
if: matrix.platform == 'ubuntu-20.04'
run: |
sudo apt-get update
sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.0-dev libappindicator3-dev librsvg2-dev patchelf

- name: install pnpm
run: npm install -g pnpm

- name: install frontend dependencies
run: pnpm install # change this to npm or pnpm depending on which one you use
- uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Binary file modified src/assets/login-image2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions src/components/auth-middleware.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useEffect } from "react";
import { useCookies } from "react-cookie";
import useAuthStore from "@/store/authStore";
import { getLoggedInUser } from "@/lib/api";
import { Icons } from "./icons";
Expand All @@ -9,7 +8,6 @@ type IAuthMiddleware = {
};

const AuthMiddleware: React.FC<IAuthMiddleware> = ({ children }) => {
const [cookies] = useCookies(["logged_in"]);
const setUser = useAuthStore((state) => state.setUser);
const [isLoading, setIsLoading] = React.useState<boolean>(true);

Expand All @@ -23,7 +21,7 @@ const AuthMiddleware: React.FC<IAuthMiddleware> = ({ children }) => {
.finally(() => {
setIsLoading(false);
});
}, [cookies.logged_in]);
}, []);

if (isLoading) {
return (
Expand Down
6 changes: 6 additions & 0 deletions src/components/avatar-dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ import { User } from "@/types";
import { useMutation } from "@tanstack/react-query";
import api from "@/lib/api";
import { Icons } from "./icons";
import useAuthStore from "@/store/authStore";
import { useNavigate } from "react-router-dom";

export function AvatarDropdownMenu({ user }: { user: User }) {
const { mutate: logoutMutate, isLoading } = useMutation(async () => {
await api.get("/auth/logout");
});
const logout = useAuthStore((state) => state.logout);
const navigate = useNavigate();

const logoutHandler = () => {
logoutMutate();
logout();
navigate(0);
};

return (
Expand Down
16 changes: 14 additions & 2 deletions src/components/user-auth-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { Icons } from "@/components/icons";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { redirect, useLocation } from "react-router-dom";
import { useLocation, useNavigate } from "react-router-dom";
import { getGoogleUrl } from "@/lib/getGoogleUrl";
import api from "@/lib/api";
import { getGitHubUrl } from "@/lib/getGithubUrl";
import useAuthStore from "@/store/authStore";

interface UserAuthFormProps extends React.HTMLAttributes<HTMLDivElement> {
login: boolean;
Expand All @@ -24,11 +25,13 @@ export function UserAuthForm({
const [password, setPassword] = React.useState<string>("");
const [confirmPassword, setConfirmPassword] = React.useState<string>("");
const [name, setName] = React.useState<string>("");
const navigate = useNavigate();

// const navigate = useNavigate();
const location = useLocation();

const from = ((location.state as any)?.from?.pathname as string) || "/";
const setAccessToken = useAuthStore((state) => state.setAccessToken);

async function onSubmit(event: React.SyntheticEvent) {
event.preventDefault();
Expand All @@ -40,7 +43,16 @@ export function UserAuthForm({
password,
});
console.log("res", res);
redirect(from);

if (res.data?.status === "success") {
const accessToken = res.data?.access_token as string;
console.log("accessToken", accessToken);

setAccessToken(accessToken);
navigate(0);
}

// redirect(from);
} else {
const data = await api.post("/auth/register", {
name,
Expand Down
32 changes: 8 additions & 24 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import useAuthStore from "@/store/authStore";
import { User } from "@/types";
import axios from "axios";

const api = axios.create({
baseURL: "https://docugenius-backend-production.up.railway.app/api",
baseURL: "https://docugenius-api.onrender.com/api",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${useAuthStore.getState().accessToken}`,
},
withCredentials: true,

});

export const refreshToken = async () => {
Expand All @@ -22,32 +22,16 @@ export const refreshToken = async () => {
}
};

api.interceptors.response.use(
(response) => {
return response;
},
async function (error) {
const originalRequest = error.config;
if (error.response?.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;

try {
await refreshToken();
} catch (e) {
return Promise.reject(e);
}

return api(originalRequest);
}
return Promise.reject(error);
}
);

export default api;

export const getLoggedInUser = async () => {
try {
const res = await api.get("/users/me");
const res = await api.get("/users/me", {
headers: {
Authorization: `Bearer ${useAuthStore.getState().accessToken}`,
},
});
return res.data.data.user as User;
} catch (e) {
console.log("error", e);
Expand Down
16 changes: 10 additions & 6 deletions src/routes/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import api from "@/lib/api";
import { Doc } from "@/types";
import { useQuery } from "@tanstack/react-query";


const DocumentsPage = () => {

const { data } = useQuery(["docs"], async () => {
const res = await api.get("/docs");
return res.data.status === "success" ? (res.data.data.docs as Doc[]) : null;
});

const { data } = useQuery(
["docs"],
async () => {
const res = await api.get("/docs");
return res.data.status === "success"
? (res.data.data.docs as Doc[])
: null;
},
{}
);

return (
<div className="w-full py-6 ">
Expand Down
24 changes: 18 additions & 6 deletions src/store/authStore.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import { User } from "@/types";
import { create } from "zustand";


import { persist } from "zustand/middleware";

type State = {
user: User | null;
accessToken: string | null;
setAccessToken: (accessToken: string | null) => void;
logout: () => void;
};

type Actions = {
setUser: (user: User | null) => void;
};

const useAuthStore = create<State & Actions>((set) => ({
user: null,
setUser: (user) => set({ user }),
}));
const useAuthStore = create(
persist<State & Actions>(
(set) => ({
user: null,
accessToken: null,
setUser: (user) => set({ user }),
setAccessToken: (accessToken) => set({ accessToken }),
logout: () => set({ user: null, accessToken: null }),
}),
{
name: "auth-storage",
}
)
);

export default useAuthStore;