Skip to content

Commit

Permalink
feat: add okta accessToken and use in request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindis committed Sep 16, 2022
1 parent 1c00574 commit 1998e8d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
15 changes: 9 additions & 6 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AppProps } from 'next/app';
import { SessionProvider } from "next-auth/react"
import React, { FC } from 'react';
import { ThemeProvider } from 'styled-components';

Expand All @@ -11,12 +12,14 @@ const App: FC<AppProps> = ({
Component,
pageProps: { session, ...pageProps }
}) => (
<ThemeProvider theme={theme}>
<GlobalStyle />
<Root>
<Component {...pageProps} />
</Root>
</ThemeProvider>
<SessionProvider session={session}>
<ThemeProvider theme={theme}>
<GlobalStyle />
<Root>
<Component {...pageProps} />
</Root>
</ThemeProvider>
</SessionProvider>
);

export default App;
16 changes: 14 additions & 2 deletions pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import NextAuth from 'next-auth';
import OktaProvider from 'next-auth/providers/okta';
export const authOptions = {
// Configure one or more authentication providers
providers: [
OktaProvider({
clientId: process.env.OKTA_CLIENT_ID,
clientSecret: process.env.NEXTAUTH_SECRET,
issuer: process.env.OKTA_ISSUER
})
]
],
callbacks: {
async session({ session, token }) {
console.log("session", token?.accessToken);
session.accessToken = token.accessToken;
return session;
},
async jwt({ account, token }) {
if (account) {
token.accessToken = account.access_token;
}
return token
}
}
};
export default NextAuth(authOptions);
27 changes: 18 additions & 9 deletions services/api/climate/host.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import axios from 'axios';
import { getSession } from 'next-auth/react';

import env from '../../../env';

interface Props {
Expand All @@ -10,15 +12,22 @@ interface Props {

const { READING_BASE_URI } = env;

export const climateReading = ({ path, method, data, params }: Props) =>
axios({
url: `${READING_BASE_URI}${path}`,
method,
data,
params
})
.then(response => response.data)
.catch(() => null);
export const climateReading = async ({ path, method, data, params }: Props) => {
const session: any = await getSession();
return (
axios({
url: `${READING_BASE_URI}${path}`,
method,
data,
params,
headers: {
Authorization: `Bearer ${session?.accessToken}`
}
})
.then(response => response.data)
.catch(() => null)
);
}

export const climateReadingPost = (path: string, body: any) =>
climateReading({ path, method: 'POST', data: body });
Expand Down

0 comments on commit 1998e8d

Please sign in to comment.