generated from amosproj/amos202Xss0Y-projname
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from amosproj/develop
connect backend and frontend context booking
- Loading branch information
Showing
19 changed files
with
372 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Session } from "next-auth"; | ||
import { IBooking } from "../../types/booking"; | ||
import { BACKEND_URL } from "./constants"; | ||
|
||
type QueryOptions = { | ||
n?: number; | ||
skip?: number; | ||
direction?: "ASC" | "DESC"; | ||
start?: EpochTimeStamp; | ||
end?: EpochTimeStamp; | ||
}; | ||
|
||
function getParams(queryOptions: QueryOptions) { | ||
const mapTable: [any, string][] = [ | ||
[queryOptions.n, "n"], | ||
[queryOptions.skip, "skip"], | ||
[queryOptions.direction, "direction"], | ||
[queryOptions.start, "start"], | ||
[queryOptions.end, "end"], | ||
]; | ||
|
||
const params = new URLSearchParams(); | ||
|
||
mapTable.forEach((val) => { | ||
if (val[0]) { | ||
params.append(val[1], val[0]); | ||
} | ||
}); | ||
|
||
return params; | ||
} | ||
|
||
export async function getBookings( | ||
session: Session, | ||
queryOptions: QueryOptions | ||
): Promise<IBooking[]> { | ||
if (!session.user) return []; | ||
|
||
const params = getParams(queryOptions); | ||
const response = await fetch(BACKEND_URL + `/bookings/range?${params}`, { | ||
headers: { | ||
Authorization: `Bearer ${session.accessToken}`, | ||
}, | ||
}); | ||
|
||
if (response.status !== 200) { | ||
return []; | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
const bookings: IBooking[] = data.map((val: any) => { | ||
return { | ||
bookingId: val.timestamp, | ||
userId: session.user.id, | ||
room: val.roomName, | ||
floor: val.floorName, | ||
building: val.buildingName, | ||
location: "N/A", | ||
...val, | ||
}; | ||
}); | ||
|
||
return bookings; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
import NextAuth from "next-auth"; | ||
import NextAuth, { Awaitable, NextAuthOptions, User } from "next-auth"; | ||
import CredentialsProvider from "next-auth/providers/credentials"; | ||
import { userAgent } from "next/server"; | ||
import { AuthResponse, authorize } from "../../../lib/api/AuthService"; | ||
import { Session } from "next-auth"; | ||
|
||
export const authOptions = { | ||
export const authOptions: NextAuthOptions = { | ||
secret: process.env.SECRET, | ||
// Configure one or more authentication providers | ||
providers: [ | ||
CredentialsProvider({ | ||
name: "Credentials", | ||
credentials: { | ||
email: { label: "E-Mailadresse", type: "text" }, | ||
password: { label: "Passwort", type: "password" }, | ||
}, | ||
async authorize(credentials, req) { | ||
async authorize(credentials, req): Promise<User | null> { | ||
// Check if credentials contains an email and password | ||
if (!credentials || !credentials.email || !credentials.password) { | ||
return null; | ||
|
@@ -35,9 +35,9 @@ export const authOptions = { | |
company: "INTERFLEX", | ||
name: "testuser", | ||
email: "[email protected]", | ||
isApproved : true, | ||
isAdmin : true, | ||
our_token: result as String, | ||
isApproved: true, | ||
isAdmin: true, | ||
our_token: result as string, | ||
}; | ||
|
||
if (user) { | ||
|
@@ -50,11 +50,25 @@ export const authOptions = { | |
// ...add more providers here | ||
], | ||
callbacks: { | ||
async session({ session }: {session: Session}) { | ||
//TODO: refactor this if me route is available | ||
// Send properties to the client, like if user is approved or is admin | ||
session.user.isApproved = true; | ||
session.user.isAdmin = true; | ||
async jwt({ token, user, account, isNewUser }) { | ||
if (user) { | ||
if (user.our_token) { | ||
token = { accessToken: user.our_token }; | ||
} | ||
} | ||
return token; | ||
}, | ||
async session({ session, token }) { | ||
session.accessToken = token.accessToken; | ||
|
||
session.user = { | ||
id: "1", | ||
name: "testuser", | ||
email: "[email protected]", | ||
accessToken: token.accessToken, | ||
isApproved: true, | ||
isAdmin: true, | ||
}; | ||
|
||
return session; | ||
}, | ||
|
Oops, something went wrong.