Important: This code is no longer being maintained. You can find the up-to-date code in the headless-templates repository.
This is a sample project that demonstrates how to authenticate users with Wix using an external identity provider (Github OAuth).
This is useful when you want to authenticate users with Wix using an external identity provider like Github OAuth. In case you already have a user base in an external identity provider, or you want to allow your users to authenticate with an external identity provider for convenience, you can use this project as a reference. This would provide your users with a seamless login experience and still leverage Wix's powerful platform for member management.
This project is a Next.js app that demonstrates how to authenticate users with Wix using Github OAuth. It uses the following technologies:
- Next.js (Basic setup)
- Github OAuth (For github authentication) with Octokit
- Wix SDK (For Wix authentication)
- Wix Members API (For member management)
Before you run this project, you need to have the following:
- A Wix account
- A Github account
- A Github App
- In your github app, be sure to set the following settings:
- Homepage URL:
http://localhost:3000 - Authorization callback URL:
http://localhost:3000/api/auth/github/callback
- Homepage URL:
- In your github app, be sure to set the following settings:
- A Wix site
- A Wix API Key with Members & Contacts permission.
To run this project, you need to follow these steps:
- Clone this repository
- Install the dependencies by running
npm install - Copy the
.env.templatefile to.env.localand fill in the required values - Run the project by running
npm run dev - Open
http://localhost:3000in your browser and sign in with your Github account - If you have set up everything correctly, you should be able to see your Wix member details and be able to update your Wix member profile slug
Signing in Wix members with an external identity provider requires a few steps:
- Perform the login with the external identity provider (Github OAuth in this case)
- This depends on the external identity provider you are using. In this project, we are using Github OAuth to authenticate users.
- Here we are following the Github OAuth web flow to authenticate users.
- We use the
@octokit/auth-apppackage to finish the web flow and get the github user access token and user info. (SeegetGithubAuthandgetGithubUserEmailinsrc/app/api/auth/github/callback/route.ts)
- Once the login process in complete, you should have the user's email available. We will be using the email as the unique identifier for the user between Wix and the external identity provider.
- Next, we want to ensure we have a Wix member created in our side for that email. We use the Wix Members API to create a member if a member with that email adress doesn't exist. (See
getOrCreateWixMemberinsrc/app/api/auth/github/callback/route.ts). Since creating a Wix member (without an explicit registration) is an admin operation, we need to use aWixClientwith an API Key that has permissions to create members. - Once we have a member id for the user, we use the
getMemberTokensForExternalLoginmethod on theOAuthStragegyto get the tokens for the user. This method will create a session for the user and return the tokens. (SeegetMemberTokensForExternalLogininsrc/app/api/auth/github/callback/route.ts). Since this is also an admin operation, it requires passing an API Key with the required permissions. - Finally, we set the token on a cookie and redirect the user to the home page. The user should now be signed in with Wix.