Hey friends, I’m building an authentication flow and currently have my front and back end working well independently. I just need to be able to hook them up together and I am having a bit of trouble, so wondering if anyone can help me spot what I’m doing wrong.
In my front end I have:
In my page:
const AuthenticationPage = () => {
return (
<>
<Toaster />
<MetaTags title="Authentication" description="Authentication page" />
<h1>Authentication Page</h1>
<GoogleOAuthProvider clientId={`${process.env.GOOGLE_CLIENT_ID}`}>
<GoogleAuth />
</GoogleOAuthProvider>
</>
)
}
The component:
export const AUTHENTICATE_USER_MUTATION = gql`
mutation AuthenticateUserMutation($code: String!) {
authenticate: authenticate(code: $code) {
accessToken
}
}
`
const GoogleAuth = ({ code }) => {
const [authenticate, { loading, error }] = useMutation(
AUTHENTICATE_USER_MUTATION,
{
onCompleted: () => {
console.log(authenticate.access_token)
},
onError: (error) => {
console.log(error.message)
},
}
)
const login = useGoogleLogin({
onSuccess: (codeResponse) => console.log(codeResponse),
flow: 'auth-code',
scope: 'https://www.googleapis.com/auth/gmail.settings.basic',
})
return (
<GoogleLogin
onSuccess={(credentialResponse) => {
login(credentialResponse)
}}
onError={() => {
console.log('Login Failed')
}}
/>
)
}
And then in the backend I just need to receive a code which I will then pass on to Google’s server and generate my access and refresh tokens
export const authenticate = async ({ code }) => {
const oAuth2Client = new OAuth2Client(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
'http://localhost:8910'
)
console.log(oAuth2Client)
const { tokens } = await oAuth2Client.getToken(code)
logger.info('output tokens')
console.log(tokens)
return {
accessToken: tokens.access_token,
refreshToken: tokens.refresh_token,
expiryDate: tokens.expiry_date,
}
}
I then have a mutation in my sdl file
type Mutation {
authenticate(code: String!): Authentication @skipAuth
}
Like I said, they both work well independently, I can get a code in the front end, and then if I use that code in Redwood GraphQL PLayground like this I’m able to get my tokens:
mutation AUTHENTICATE($code: String!) {
authenticate(code: $code) {
accessToken
refreshToken
expiryDate
}
}
I just can’t get my front and back end to talk to each other which I know is a 100% down to how I’m trying to do that in my component, I just don;lt know how to move forward from here.
Thanks in advance