Skip to content

Commit

Permalink
userId
Browse files Browse the repository at this point in the history
  • Loading branch information
xdzqyyds committed Nov 29, 2024
1 parent 079df30 commit 9bdb67e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
4 changes: 2 additions & 2 deletions server/api/v1/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type LoginRequest struct {
Username string `json:"username"`
UserId string `json:"userId"`
Password string `json:"password"`
}

Expand All @@ -34,7 +34,7 @@ func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {

// get password
ctx := context.Background()
passwordKey := loginReq.Username
passwordKey := loginReq.UserId
log.Printf("Attempting to fetch password for key: %s\n", passwordKey)
password, err := h.rdb.HGet(ctx, userStorageKey, passwordKey).Result()
if err != nil {
Expand Down
59 changes: 52 additions & 7 deletions server/daemon/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,64 @@ func initUserData(rdb *redis.Client) {

// Redis hash
userStorageKey := "user_storage"
userOnlineStatusKey := "user_online_status"

// users storage with key as username and value as password
users := map[string]string{
"a": "aaa",
"b": "bbb",
"c": "ccc",
"d": "ddd",
}
// users storage with key as userId and value as password
users := generateUsers()

// add to Redis
for user, password := range users {
if err := rdb.HSet(ctx, userStorageKey, user, password).Err(); err != nil {
log.Printf("Failed to set user %s data: %v\n", user, err)
}
}

// online status storage with key as userId and value as online status
onlineStatus := generateOnlineStatus()

// add to Redis
for user, status := range onlineStatus {
if err := rdb.HSet(ctx, userOnlineStatusKey, user, status).Err(); err != nil {
log.Printf("Failed to set user %s online status: %v\n", user, err)
}
}

}

func generateUsers() map[string]string {
users := make(map[string]string)

// 生成从 a 到 z 的用户名和密码
for i := 'a'; i <= 'z'; i++ {
userId := string(i)
password := userId + userId + userId
users[userId] = password
}

// 生成从 A 到 Z 的用户名和密码
for i := 'A'; i <= 'Z'; i++ {
userId := string(i)
password := userId + userId + userId
users[userId] = password
}

return users
}

func generateOnlineStatus() map[string]bool {
onlineStatus := make(map[string]bool)

// 生成从 a 到 z 的在线状态
for i := 'a'; i <= 'z'; i++ {
userId := string(i)
onlineStatus[userId] = false
}

// 生成从 A 到 Z 的在线状态
for i := 'A'; i <= 'Z'; i++ {
userId := string(i)
onlineStatus[userId] = false
}

return onlineStatus
}
25 changes: 12 additions & 13 deletions webapp/components/join.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ import {
import { getStorage, setStorage, delStorage, setStorageStream, setStorageMeeting } from '../lib/storage'
import { newRoom, newUser, setApiToken, setRoomId } from '../lib/api'

export const getLoginStatus = async () => {
const user = getStorage()
if (!user.token || !user.stream) {
const res = await newUser()
user.token = res.token
user.stream = res.streamId
setStorage(user)
}

setApiToken(user.token)
if (user.stream) setStorageStream(user.stream)
}
export default function Join() {
const [loc, setLoc] = useAtom(locationAtom)
const [__, setAtomMeetingId] = useAtom(meetingIdAtom)
const [tmpId, setTmpId] = useState<string>('')

const getLoginStatus = async () => {
const user = getStorage()
if (!user.token || !user.stream) {
const res = await newUser()
user.token = res.token
user.stream = res.streamId
setStorage(user)
}

setApiToken(user.token)
if (user.stream) setStorageStream(user.stream)
}

const newMeeting = async () => {
await getLoginStatus()
const meetingId = (await newRoom()).roomId
Expand Down
2 changes: 2 additions & 0 deletions webapp/components/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState } from 'react'
import { useAtom } from 'jotai'
import { userIdAtom, userPasswordAtom, isLoggedInAtom } from '../store/atom'
import Join from '../components/join'
import { getLoginStatus } from '../components/join'
import { login } from '../lib/api'

export default function Login() {
Expand All @@ -20,6 +21,7 @@ export default function Login() {
if (response.success) {
setError(null)
setIsLoggedIn(true)
await getLoginStatus()
} else {
setError(response.message || 'Login failed')
}
Expand Down
Empty file added webapp/components/userlist.tsx
Empty file.
4 changes: 2 additions & 2 deletions webapp/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ async function delStream(roomId: string, streamId: string): Promise<any> {
})
}

async function login(username: string, password: string): Promise<{ success: boolean; message: string }> {
async function login(userId: string, password: string): Promise<{ success: boolean; message: string }> {
return (await fetch('/login/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
body: JSON.stringify({ userId, password }),
})).json()
}

Expand Down

0 comments on commit 9bdb67e

Please sign in to comment.