Skip to content

gonextgames/utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Next Games Landing Utils

This is a collection of utilities for the Go Next Games Landing Page.

Development

In your package.json, add the following:

"@gonextgames/utils-client": "file:/Users/oliverbarnum/Documents/git/utils/client",
"@gonextgames/utils-server": "file:/Users/oliverbarnum/Documents/git/utils/server",

Then run npm install

Create an .env file:

AWS_ACCESS_KEY=""
AWS_SECRET=""
ENVIRONMENT=""
JWT_SECRET_KEY=""
STRIPE_API=""
STRIPE_PUBLIC_KEY=""
OPENAI_API_KEY=""

Then

cd ./client
npm run dev

New Terminal:

cd ./server
npm run dev

Auth

Client Side Consumption

  • Wrap the layout in <AuthProvider></AuthProvider>
  • In components that need to know whether you are auth'd:
import { useAuth, trackEvent } from '@gonextgames/utils-client'
const { isAuthenticated, logout, user } = useAuth()
  • To change auth
import { useAuth } from '@gonextgames/utils/client'
// Call these functions to call the backend
const { login/register/logout } = useAuth()

When implementing auth, you can use the redirect function with the from parameter to redirect after logging in. After registering, you can redirect to the getting started page or whatever is appropriate for your application.

Server Side Consumption

  • Implement /api/auth/verify for tokens
import { getUserFromToken } from '@gonextgames/utils/server/auth'

export async function GET(req) {
  try {
    const user = await getUserFromToken("NAME_OF_USERS_TABLE", "TOKEN_NAME")
    if (user === null) {
      return Response.json({authenticated: false}, { status: 200 })
    }
    return Response.json({authenticated: true, user: user})
  } catch (error) {
    console.error('Verify token error:', error)
    return Response.json({ authenticated: false, error: 'Token verification failed' }, { status: 401 })
  }
} 
  • Consume it with
import { getUserFromToken } from '@gonextgames/utils/server/auth'
const user = await getUserFromToken("NAME_OF_USERS_TABLE", "TOKEN_NAME")
  if (!user) {
    redirect(`/login?from=${encodeURIComponent('/FROM-ENDPOINT')}`)
  }
  • Create /api/auth/login /logout and /register
// Register
const user = await registerUser(userData.name, userData.email, userData.password, { link: userData.link }, 'bgp_users')

// Login
const user = await loginWithEmailAndPasswordAndSetToken(email, password, 'bgp_users', "token")

//Logout 
const cookieStore = await cookies()

cookieStore.set('token', '', {
httpOnly: true,
secure: process.env.ENVIRONMENT === 'production',
sameSite: 'lax',
path: '/',
expires: new Date(0)
})

Users Table

The user table is standardized. Calling CRUD for users is handled by the package, where you pass the TABLE_NAME to the function every time you want to call it.

await updateUser(user.user_id, {
    contact_email: email,
}, 'bgp_users')

Nothing is stopping you from creating new ways to get the information, such as by the users /link, like in Board Game Prototypes.

The users table must have:

  • user_id
  • email
  • name
  • password

And a email-user_id-index secondary index.

Event Tracking

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published