ui: align js with api
This commit is contained in:
+5
-7
@@ -36,6 +36,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import Alpine from 'alpinejs'
|
import Alpine from 'alpinejs'
|
||||||
|
import { requireAuth } from '/src/auth.js'
|
||||||
import { logout, getchannels, getmessages } from '/src/api.js'
|
import { logout, getchannels, getmessages } from '/src/api.js'
|
||||||
|
|
||||||
window.app = () => ({
|
window.app = () => ({
|
||||||
@@ -45,12 +46,9 @@
|
|||||||
selectedChannel: null,
|
selectedChannel: null,
|
||||||
messages: [],
|
messages: [],
|
||||||
async init() {
|
async init() {
|
||||||
const res = await fetch('/api/whoami')
|
const username = await requireAuth()
|
||||||
if (res.status === 401) {
|
if (!username) return
|
||||||
window.location.href = '/login.html'
|
this.username = username
|
||||||
return
|
|
||||||
}
|
|
||||||
this.username = await res.text()
|
|
||||||
try {
|
try {
|
||||||
this.channels = await getchannels()
|
this.channels = await getchannels()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -62,7 +60,7 @@
|
|||||||
this.messages = []
|
this.messages = []
|
||||||
this.error = ''
|
this.error = ''
|
||||||
try {
|
try {
|
||||||
const from = new Date(Date.now() - 24 * 60 * 60 * 1000)
|
const from = new Date(Date.now() - 96 * 60 * 60 * 1000)
|
||||||
const to = new Date(Date.now())
|
const to = new Date(Date.now())
|
||||||
this.messages = await getmessages(ch.ID, { from, to })
|
this.messages = await getmessages(ch.ID, { from, to })
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
+62
-6
@@ -1,28 +1,84 @@
|
|||||||
|
const BASE = __API_URL__
|
||||||
|
const opts = { credentials: 'include' }
|
||||||
|
|
||||||
export async function login(username, password) {
|
export async function login(username, password) {
|
||||||
const form = new FormData()
|
const form = new FormData()
|
||||||
form.append('username', username)
|
form.append('username', username)
|
||||||
form.append('password', password)
|
form.append('password', password)
|
||||||
const res = await fetch('/api/login', { method: 'POST', body: form })
|
const res = await fetch(`${BASE}/login`, { ...opts, method: 'POST', body: form })
|
||||||
if (!res.ok) throw new Error(await res.text())
|
if (!res.ok) throw new Error(await res.text())
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function logout() {
|
export async function logout() {
|
||||||
const res = await fetch('/api/logout', { method: 'POST' })
|
const res = await fetch(`${BASE}/logout`, { ...opts, method: 'POST' })
|
||||||
if (!res.ok) throw new Error(await res.text())
|
if (!res.ok) throw new Error(await res.text())
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getchannels() {
|
export async function register(name, password) {
|
||||||
const res = await fetch('/api/channels')
|
const form = new FormData()
|
||||||
|
form.append('name', name)
|
||||||
|
form.append('password', password)
|
||||||
|
const res = await fetch(`${BASE}/register`, { ...opts, method: 'POST', body: form })
|
||||||
if (!res.ok) throw new Error(await res.text())
|
if (!res.ok) throw new Error(await res.text())
|
||||||
return res.json()
|
return res.json()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getchannels() {
|
||||||
|
const res = await fetch(`${BASE}/channels`, opts)
|
||||||
|
if (!res.ok) throw new Error(await res.text())
|
||||||
|
return res.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getchannel(channelid) {
|
||||||
|
const res = await fetch(`${BASE}/channels/${channelid}`, opts)
|
||||||
|
if (!res.ok) throw new Error(await res.text())
|
||||||
|
return res.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createchannel({ name, type = '', location = '', notes = '' }) {
|
||||||
|
const form = new FormData()
|
||||||
|
form.append('name', name)
|
||||||
|
form.append('type', type)
|
||||||
|
form.append('location', location)
|
||||||
|
form.append('notes', notes)
|
||||||
|
const res = await fetch(`${BASE}/channels`, { ...opts, method: 'POST', body: form })
|
||||||
|
if (!res.ok) throw new Error(await res.text())
|
||||||
|
return res.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deletechannel(channelid) {
|
||||||
|
const res = await fetch(`${BASE}/channels/${channelid}`, { ...opts, method: 'DELETE' })
|
||||||
|
if (!res.ok) throw new Error(await res.text())
|
||||||
|
}
|
||||||
|
|
||||||
export async function getmessages(channelid, { from, to } = {}) {
|
export async function getmessages(channelid, { from, to } = {}) {
|
||||||
const params = new URLSearchParams()
|
const params = new URLSearchParams()
|
||||||
if (from) params.set('from', from.toISOString())
|
if (from) params.set('from', from.toISOString())
|
||||||
if (to) params.set('to', to.toISOString())
|
if (to) params.set('to', to.toISOString())
|
||||||
const query = params.size ? `?${params}` : ''
|
const query = params.size ? `?${params}` : ''
|
||||||
const res = await fetch(`/api/channels/${channelid}/messages${query}`)
|
const res = await fetch(`${BASE}/channels/${channelid}/messages${query}`, opts)
|
||||||
if (!res.ok) throw new Error(await res.text())
|
if (!res.ok) throw new Error(await res.text())
|
||||||
return res.json()
|
return res.json()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getmessage(channelid, messageid) {
|
||||||
|
const res = await fetch(`${BASE}/channels/${channelid}/messages/${messageid}`, opts)
|
||||||
|
if (!res.ok) throw new Error(await res.text())
|
||||||
|
return res.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getfileurl(fileid) {
|
||||||
|
return `${BASE}/files/${fileid}`
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getusers() {
|
||||||
|
const res = await fetch(`${BASE}/users`, opts)
|
||||||
|
if (!res.ok) throw new Error(await res.text())
|
||||||
|
return res.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getuser(userid) {
|
||||||
|
const res = await fetch(`${BASE}/users/${userid}`, opts)
|
||||||
|
if (!res.ok) throw new Error(await res.text())
|
||||||
|
return res.json()
|
||||||
|
}
|
||||||
|
|||||||
+6
-2
@@ -1,12 +1,16 @@
|
|||||||
|
const BASE = __API_URL__
|
||||||
|
|
||||||
export async function requireAuth() {
|
export async function requireAuth() {
|
||||||
const res = await fetch('/api/whoami')
|
const res = await fetch(`${BASE}/whoami`, { credentials: 'include' })
|
||||||
if (res.status === 401) {
|
if (res.status === 401) {
|
||||||
window.location.href = '/login.html'
|
window.location.href = '/login.html'
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
return res.text()
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function redirectIfAuthed() {
|
export async function redirectIfAuthed() {
|
||||||
const res = await fetch('/api/whoami')
|
const res = await fetch(`${BASE}/whoami`, { credentials: 'include' })
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
window.location.href = '/'
|
window.location.href = '/'
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-7
@@ -1,17 +1,14 @@
|
|||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from 'vite'
|
||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
|
|
||||||
const API_URL = process.env.API_URL ?? 'http://192.168.56.1:3000'
|
const API_URL = process.env.API_URL ?? 'http://localhost:3000'
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
define: {
|
||||||
|
__API_URL__: JSON.stringify(API_URL),
|
||||||
|
},
|
||||||
server: {
|
server: {
|
||||||
port: 5173,
|
port: 5173,
|
||||||
proxy: {
|
|
||||||
'/api': {
|
|
||||||
target: API_URL,
|
|
||||||
rewrite: (path) => path.replace(/^\/api/, ''),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
build: {
|
build: {
|
||||||
outDir: 'dist',
|
outDir: 'dist',
|
||||||
|
|||||||
Reference in New Issue
Block a user