oneloginschematics v1.0.0
OktaDev Schematics
Fast and easy installation of Okta's OIDC SDKs
This project is a Schematics implementation that allows you to easily integrate Okta into your Angular, React, Vue, Ionic, React Native, and Express projects.
This library currently supports:
Prerequisites: Node.js.
Angular
First, create an empty project with Angular CLI. You must add Angular routing for this schematic to work.
npm i -g @angular/cli
ng new secure-angular --routing
cd secure-angularAdd an OpenID Connect App in Okta
- Install the Okta CLI
- Run
okta registerto create an account, followed byokta apps create - Choose Single Page App and use
http://localhost:4200/callbackfor the Redirect URI
In your secure-angular project, add @oktadev/schematics:
ng add @oktadev/schematicsUse the values that the Okta CLI provides for the issuer and client ID when prompted.
Run npm start, open http://localhost:4200 in your browser, and sign in. š„³
See the Okta Angular SDK for more information.
You can also use the Okta Admin Console:
- Log in to your Okta instance (or create an account if you don't have one). Go to Applications > Create App Integration > OIDC.
- Choose Single-Page Application as the application type and click Next.
- Add
http://localhost:4200/callbackas a Login redirect URI andhttp://localhost:4200as a Logout redirect URI. - Specify
http://localhost:4200as a Trusted Origin and click Save.
React
Create a new project with Create React App.
npx create-react-app secure-react
cd secure-reactIf you'd like to use TypeScript, add the --template typescript flag.
npx create-react-app secure-react --template typescript
cd secure-reactAdd an OpenID Connect App in Okta
- Install the Okta CLI
- Run
okta registerto create an account, followed byokta apps create - Choose Single Page App and use
http://localhost:3000/callbackfor the Redirect URI
Install the Schematics CLI globally.
npm install -g @angular-devkit/schematics-cliThen install and run the add-auth schematic in your secure-react project.
npm i -D @oktadev/schematics
schematics @oktadev/schematics:add-authUse the values that the Okta CLI provides for the issuer and client ID when prompted.
Run npm start, open http://localhost:3000 in your browser, and sign in. š
See the Okta React SDK for more information.
You can also use the Okta Admin Console:
- Log in to your Okta instance (or create an account if you don't have one). Go to Applications > Create App Integration > OIDC.
- Choose Single-Page Application as the application type and click Next.
- Add
http://localhost:3000/callbackas a Login redirect URI andhttp://localhost:3000as a Logout redirect URI. - Add
http://localhost:3000as a Trusted Origin and click Save.
Vue
Create a new project with Vue CLI. You must add routing for this schematic to work. If you specify TypeScript, a src/router/index.ts will be used.
npm i -g @vue/cli
vue create secure-vue
cd secure-vueAdd an OpenID Connect App in Okta
- Install the Okta CLI
- Run
okta registerto create an account, followed byokta apps create - Choose Single Page App and use
http://localhost:8080/callbackfor the Redirect URI
Install the Schematics CLI globally.
npm install -g @angular-devkit/schematics-cliThen install and run the add-auth schematic in your secure-vue project.
npm i -D @oktadev/schematics
schematics @oktadev/schematics:add-authUse the values that the Okta CLI provides for the issuer and client ID when prompted.
Run npm run serve, open http://localhost:8080 in your browser, and sign in. š„
See the Okta Vue SDK for more information.
You can also use the Okta Admin Console:
- Log in to your Okta instance (or create an account if you don't have one). Go to Applications > Create App Integration > OIDC.
- Choose Single-Page Application as the application type and click Next.
- Add
http://localhost:8080/callbackas a Login redirect URI andhttp://localhost:8080as a Logout redirect URI. - Add
http://localhost:8080as a Trusted Origin and click Save.
Ionic
Create a new Ionic + Angular project with Ionic CLI. You must use the tabs layout for everything to work currently.
npm install -g @ionic/cli
ionic start secure-ionic tabs --type=angular --no-interactive
cd secure-ionicYou will need an issuer and a clientId to begin. You can obtain those from Okta by completing the following steps.
NOTE: OIDC Login for Ionic is possible thanks to the excellent Ionic AppAuth project and its examples. This integration is not Okta-specific and should work with any identity provider that supports PKCE for browser and mobile apps.
Create an Application in Okta
- Install the Okta CLI
- Run
okta registerto create an account, followed byokta apps create - Choose Native and use
[com.okta.dev-737523:/callback,http://localhost:8100/callback]for the Login redirect URIs (wheredev-737523.okta.comis your Okta Org URL) - Use
[com.okta.dev-737523:/logout,http://localhost:8100/logout]for the Login redirect URIs
In your secure-ionic project, add @oktadev/schematics:
ng add @oktadev/schematicsUse the values that the Okta CLI provides for the issuer and client ID when prompted.
NOTE: You can switch to Cordova by passing in --platform=cordova. The default is Capacitor.
Start your app and authenticate with Okta. š
ionic serveYou can also use the Okta Admin Console:
Log in to your Okta instance (or sign up if you don't have an account).
From the Applications page, choose Create App Integration > OIDC. Select Native Application.
Give your app a memorable name, and configure it as follows:
- Login redirect URIs:
http://localhost:8100/callbackcom.okta.dev-737523:/callback(wheredev-737523.okta.comis your Okta Org URL)
- Logout redirect URIs:
http://localhost:8100/logoutcom.okta.dev-737523:/logout
- Trusted Origins:
http://localhost:8100
- Click Save
iOS
If you ran ng add @oktadev/schematics without a --platform parameter, your project has been configured for Capacitor. Build and add Capacitor for iOS with the following commands:
ionic build
npm i @capacitor/ios
npx cap add iosAdd your custom scheme to ios/App/App/Info.plist:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.getcapacitor.capacitor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>capacitor</string>
<string>com.okta.dev-737523</string>
</array>
</dict>
</array>Then, run your project using the Capacitor CLI:
npx cap run iosYou can also open your project in Xcode and configure code signing.
npx cap open iosThen run your app from Xcode.
iOS and Cordova
If you want to use Cordova, you must integrate this library with ng add @oktadev/schematics --platform=cordova.
Then, generate a native project with the following command:
ionic cordova prepare iosOpen your project in Xcode, configure code signing, and run your app.
open platforms/ios/MyApp.xcworkspaceAndroid
If you ran ng add @oktadev/schematics without a --platform parameter, your project has been configured for Capacitor. Build and add Capacitor for Android with the following commands:
ionic build
npm i @capacitor/android
npx cap add androidChange the custom scheme in android/app/src/main/res/values/strings.xml to use your reverse domain name:
<string name="custom_url_scheme">com.okta.dev-737523</string>The SafariViewController Cordova Plugin is installed as part of this project. Capacitor uses AndroidX dependencies, but the SafariViewController plugin uses an older non-AndroidX dependency. Use jetifier to patch usages of old support libraries with the following commands:
npm install jetifier
npx jetify
npx cap sync androidThen, run your project using the Capacitor CLI:
npx cap run androidYou can also open your project in Android Studio and run your app.
npx cap open androidAndroid and Cordova
If you want to use Cordova, you must add this library with ng add @oktadev/schematics --platform=cordova.
Then, generate a native project with the following command:
ionic cordova prepare androidSet the launchMode to singleTask so the URL does not trigger a new instance of the app in platforms/android/app/src/main/AndroidManifest.xml:
android:launchMode="singleTask"Open platforms/android in Android Studio and run your app.
If you get a Gradle error in Android Studio, go to File > Project Structure and change the Android Gradle Plugin to 4.0.1. Kudos to Stack Overflow.
See Ionic's iOS and Android Development docs for more information.
React Native
Create a new React Native project with the React Native CLI.
npx react-native init SecureAppYou will need an issuer and a clientId to begin. You can obtain those from Okta by completing the following steps.
Create an Application in Okta
- Install the Okta CLI
- Run
okta registerto create an account, followed byokta apps create - Choose Native and accept the default Redirect URI
- Modify the Post Logout Redirect URI to match the default Redirect URI (e.g.,
com.okta.dev-123456:/callback).
Install the Schematics CLI globally.
npm install -g @angular-devkit/schematics-cliInstall and run the add-auth schematic in your SecureApp project.
cd SecureApp
npm i -D @oktadev/schematics
schematics @oktadev/schematics:add-auth --issuer=$issuer --clientId=$clientIdYou can also use the Okta Admin Console:
- Log in to your Okta instance (or create an account if you don't have one). Go to Applications > Create App Integration > OIDC.
- Choose Native as the application type and click Next.
- Add
com.okta.dev-123456:/callbackas a Login redirect URI andcom.okta.dev-123456:/callbackas a Logout redirect URI (wheredev-123456.okta.comis your Okta domain).
iOS
Run pod install --project-directory=ios to install the Okta OIDC iOS SDK.
Start your app and authenticate with Okta. š
npm run iosAndroid
One change is made to Android build files to integrate the Okta OIDC Android SDK.
- In
android/app/build.gradle, anappAuthRedirectSchemeis added inandroid>defaultConfig.
Since this modification is done for you, you can simply start your app and authenticate with Okta. š
npm run androidFor more information, see the Okta React Native SDK documentation.
Express
Create a new project with express-generator and pug.
mkdir express-app
cd express-app
npx express-generator --view=pugAdd an OpenID Connect App in Okta
- Install the Okta CLI
- Run
okta registerto create an account, followed byokta apps create - Choose Web > Other and use
http://localhost:3000/callbackfor the Redirect URI - Accept the default Post Logout Redirect URI (
http://localhost:3000)
Install the Schematics CLI globally.
npm install -g @angular-devkit/schematics-cliThen install and run the add-auth schematic in your express-app project.
The Okta CLI will create an .okta.env file in the current directory. It will have the values you need. After you use them in the command below, you can delete this file.
npm i -D @oktadev/schematics
schematics @oktadev/schematics:add-auth --issuer=$issuer \
--clientId=$clientId --clientSecret=$clientSecretšØ This process will create an .env file will be generated with your Okta credentials. Make sure to add *.env to .gitignore and don't check it into source control!
Start your app and authenticate with Okta at http://localhost:3000. š
npm startSee the Okta OIDC Middleware SDK for more information.
You can also create your app using the Okta Admin Console:
- Log into the Okta Developer Dashboard (or create an account if you don't have one), click Applications then Create App Integration > OIDC.
- Choose Web as the application type and click Next.
- Add a Login redirect URI of
http://localhost:3000/callback. - Add a Logout redirect URI of
http://localhost:3000. - Click Save.
Testing
This project supports unit tests and integration tests.
npm test will run the unit tests, using Jasmine as a runner and test framework.
./test-app.sh angular will create an Angular project with Angular CLI, install this project, and make sure all the project's tests pass. Other options include react, react-ts, vue, vue-ts, ionic, ionic-cordova, react-native, and express.
./test-all.sh will test all the options: Angular, React, React with TypeScript, Vue, Vue with TypeScript, Ionic with Cordova, Ionic with Capacitor, React Native, and Express.
Publishing
To publish, simply do:
npm publishThat's it!
Contributing
If you'd like to modify this library, and contribute your changes, you can start by forking it to your own GitHub repository. Then, clone it to your hard drive.
git clone git@github.com:<your username>/schematics.git
cd schematicsCreate a new branch for your changes:
git checkout -b my-awesome-branchMake the changes you want to make and add tests where appropriate. Create a new project with whatever framework you're using, then run the following command inside it to use your modified project.
npm link /path/to/schematicsYou'll need to run npm run build whenever you change anything in the schematics project.
NOTE: You can also use npm pack in your schematics project, then npm install /path/to/artifact.tar.gz in your test project. This mimics npm install more than npm link.
Tutorials
See the following blog posts to see OktaDev Schematics in action.
- Use Angular Schematics to Simplify Your Life
- Use Schematics with Vue and Add Authentication in 5 Minutes
- Use Schematics with React and Add OpenID Connect Authentication in 5 Minutes
- Tutorial: User Login and Registration in Ionic 4
- Create a React Native App with Login in 10 Minutes
Links
This project uses the following open source libraries from Okta:
For Ionic, it uses Ionic AppAuth.
Help
Please post any questions as issues or ask them on the Okta Developer Forums.
License
Apache 2.0, see LICENSE.
5 years ago