-
-
Notifications
You must be signed in to change notification settings - Fork 136
feat: add Vue Router support #390
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createORPCClient, createORPCFetchHandler } from "@orpc/client"; | ||
import { ref } from "vue"; | ||
import type { ORPCContext, orpc } from "../../../../server/src/lib/orpc"; | ||
|
||
export const getAuthToken = () => { | ||
if (typeof window === "undefined") return undefined; | ||
return localStorage.getItem("auth-token") ?? undefined; | ||
}; | ||
|
||
export const authToken = ref(getAuthToken()); | ||
|
||
const fetchHandler = createORPCFetchHandler({ | ||
baseURL: import.meta.env.VITE_SERVER_URL ?? "http://localhost:5000/api", | ||
headers: () => { | ||
const token = authToken.value; | ||
return token ? { Authorization: `Bearer ${token}` } : undefined; | ||
}, | ||
}); | ||
|
||
export const { api } = createORPCClient< | ||
typeof orpc, | ||
ORPCContext | ||
>({ | ||
async: fetchHandler, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { createTRPCProxyClient, httpBatchLink } from "@trpc/client"; | ||
import superjson from "superjson"; | ||
import type { AppRouter } from "../../../../server/src/router/root"; | ||
|
||
export const api = createTRPCProxyClient<AppRouter>({ | ||
links: [ | ||
httpBatchLink({ | ||
transformer: superjson, | ||
url: import.meta.env.VITE_SERVER_URL ?? "http://localhost:5000/api/trpc", | ||
async headers() { | ||
const headers: Record<string, string> = {}; | ||
const token = localStorage.getItem("auth-token"); | ||
if (token) { | ||
headers.authorization = `Bearer ${token}`; | ||
} | ||
return headers; | ||
}, | ||
}), | ||
], | ||
}); |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,89 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { ref, computed } from 'vue' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { useRouter } from 'vue-router' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { api{{#if (eq api "trpc")}}, authToken{{/if}} } from './api' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export interface User { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
id: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
email: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name: string | null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export interface Session { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
user: User | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expiresAt: Date | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const currentUser = ref<User | null>(null) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const isLoading = ref(true) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Move reactive state inside the composable function. The Move the reactive state inside the composable function: +export function useAuth() {
+ const currentUser = ref<User | null>(null)
+ const isLoading = ref(true)
+ const router = useRouter()
-const currentUser = ref<User | null>(null)
-const isLoading = ref(true)
-export function useAuth() {
- const router = useRouter() 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export function useAuth() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const router = useRouter() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const isAuthenticated = computed(() => !!currentUser.value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const initialize = async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const token = localStorage.getItem('auth-token') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!token) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
isLoading.value = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { data } = await api.auth.getSession{{#if (eq api "orpc")}}(){{/if}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (data) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
currentUser.value = data.user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{{#if (eq api "orpc")}} authToken.value = token{{/if}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
localStorage.removeItem('auth-token') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{{#if (eq api "orpc")}} authToken.value = null{{/if}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} finally { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
isLoading.value = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const login = async (email: string, password: string) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { data } = await api.auth.login{{#if (eq api "orpc")}}({email, password}){{else}}({ email, password }){{/if}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (data?.token) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
localStorage.setItem('auth-token', data.token) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{{#if (eq api "orpc")}} authToken.value = data.token{{/if}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
currentUser.value = data.user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
router.push('/') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+45
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for authentication failures. The login function should handle potential authentication failures and provide user feedback. Add error handling: const login = async (email: string, password: string) => {
+ try {
const { data } = await api.auth.login{{#if (eq api "orpc")}}({email, password}){{else}}({ email, password }){{/if}}
if (data?.token) {
localStorage.setItem('auth-token', data.token)
{{#if (eq api "orpc")}} authToken.value = data.token{{/if}}
currentUser.value = data.user
router.push('/')
+ } else {
+ throw new Error('Authentication failed')
}
+ } catch (error) {
+ console.error('Login error:', error)
+ throw error
+ }
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const signup = async (email: string, password: string, name: string) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { data } = await api.auth.signup{{#if (eq api "orpc")}}({email, password, name}){{else}}({ email, password, name }){{/if}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (data?.token) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
localStorage.setItem('auth-token', data.token) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{{#if (eq api "orpc")}} authToken.value = data.token{{/if}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
currentUser.value = data.user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
router.push('/') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+56
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for signup failures. Similar to the login function, signup should handle potential failures. Apply the same error handling pattern as suggested for the login function. 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const logout = async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await api.auth.logout{{#if (eq api "orpc")}}(){{/if}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.error('Logout error:', error) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} finally { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
localStorage.removeItem('auth-token') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{{#if (eq api "orpc")}} authToken.value = null{{/if}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
currentUser.value = null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
router.push('/login') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
user: currentUser, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
isAuthenticated, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
isLoading, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
initialize, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
login, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
signup, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export default { | ||
async fetch(request, env) { | ||
const url = new URL(request.url); | ||
|
||
// Serve static assets | ||
try { | ||
// Check if the path has a file extension | ||
const hasExtension = url.pathname.split('/').pop().includes('.'); | ||
|
||
if (hasExtension) { | ||
// Try to serve the static asset | ||
return await env.ASSETS.fetch(request); | ||
} | ||
|
||
// For paths without extensions, serve index.html (SPA routing) | ||
const indexRequest = new Request(new URL('/index.html', request.url).toString(), request); | ||
return await env.ASSETS.fetch(indexRequest); | ||
} catch (e) { | ||
// If the asset doesn't exist, serve index.html | ||
const indexRequest = new Request(new URL('/index.html', request.url).toString(), request); | ||
return await env.ASSETS.fetch(indexRequest); | ||
} | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,8 @@ | ||||||||
{ | ||||||||
"scripts": { | ||||||||
"deploy": "wrangler pages deploy dist" | ||||||||
}, | ||||||||
"devDependencies": { | ||||||||
"wrangler": "^3.101.0" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix version mismatch with constants.ts. The wrangler version here ( Apply this diff to use the consistent version: - "wrangler": "^3.101.0"
+ "wrangler": "^4.23.0" 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||
} | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flag outdated alias:
native
is not a valid--frontend
valueThe option list correctly shows
native-nativewind
andnative-unistyles
, but the sample command later uses--frontend … native
(singular).Leaving the alias will cause runtime validation errors.
Search/replace the sample invocation:
🤖 Prompt for AI Agents