Skip to content

Commit ef6c17a

Browse files
authored
Merge pull request #764 from reactioncommerce/akarshit-revert-account-js
revert: "feat: breaking use account-js for authentication"
2 parents b1c1ae0 + 6948fe0 commit ef6c17a

34 files changed

+2447
-3057
lines changed

.env.example

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
CANONICAL_URL=http://localhost:4000
2+
ENABLE_SPA_ROUTING=true
23
BUILD_GRAPHQL_URL=http://localhost:3000/graphql
34
EXTERNAL_GRAPHQL_URL=http://localhost:3000/graphql
45
INTERNAL_GRAPHQL_URL=http://api.reaction.localhost:3000/graphql
6+
OAUTH2_ADMIN_PORT=4445
7+
OAUTH2_ADMIN_URL=http://hydra.reaction.localhost:4445
8+
OAUTH2_AUTH_URL=http://localhost:4444/oauth2/auth
9+
OAUTH2_CLIENT_ID=example-storefront
10+
OAUTH2_CLIENT_SECRET=CHANGEME
11+
OAUTH2_PUBLIC_LOGOUT_URL=http://localhost:4444/oauth2/sessions/logout
12+
OAUTH2_HOST=hydra.reaction.localhost
13+
OAUTH2_IDP_PUBLIC_CHANGE_PASSWORD_URL=http://localhost:4100/account/change-password?email=EMAIL&from=FROM
14+
OAUTH2_IDP_HOST_URL=http://identity.reaction.localhost:4100
15+
OAUTH2_TOKEN_URL=http://hydra.reaction.localhost:4444/oauth2/token
516
PORT=4000
617
SEGMENT_ANALYTICS_SKIP_MINIMIZE=true
718
SEGMENT_ANALYTICS_WRITE_KEY=ENTER_KEY_HERE
819
SESSION_MAX_AGE_MS=2592000000
920
SESSION_SECRET=CHANGEME
10-
STRIPE_PUBLIC_API_KEY=ENTER_STRIPE_PUBLIC_KEY_HERE
21+
STRIPE_PUBLIC_API_KEY=ENTER_STRIPE_PUBLIC_KEY_HERE

.env.prod

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
CANONICAL_URL=http://localhost:4000
2+
ENABLE_SPA_ROUTING=true
23
BUILD_GRAPHQL_URL=http://localhost:3000/graphql
34
EXTERNAL_GRAPHQL_URL=http://localhost:3000/graphql
45
INTERNAL_GRAPHQL_URL=http://api.reaction.localhost:3000/graphql
6+
OAUTH2_ADMIN_PORT=4445
7+
OAUTH2_ADMIN_URL=http://hydra.reaction.localhost:4445
8+
OAUTH2_AUTH_URL=http://localhost:4444/oauth2/auth
9+
OAUTH2_CLIENT_ID=example-storefront
10+
OAUTH2_CLIENT_SECRET=CHANGEME
11+
OAUTH2_PUBLIC_LOGOUT_URL=http://localhost:4444/oauth2/sessions/logout
12+
OAUTH2_HOST=hydra.reaction.localhost
13+
OAUTH2_IDP_PUBLIC_CHANGE_PASSWORD_URL=http://localhost:4100/account/change-password?email=EMAIL&from=FROM
14+
OAUTH2_IDP_HOST_URL=http://identity.reaction.localhost:4100
15+
OAUTH2_TOKEN_URL=http://hydra.reaction.localhost:4444/oauth2/token
516
PORT=4000
617
SEGMENT_ANALYTICS_SKIP_MINIMIZE=true
718
SEGMENT_ANALYTICS_WRITE_KEY=ENTER_KEY_HERE
819
SESSION_MAX_AGE_MS=2592000000
920
SESSION_SECRET=CHANGEME
10-
STRIPE_PUBLIC_API_KEY=ENTER_STRIPE_PUBLIC_KEY_HERE
21+
STRIPE_PUBLIC_API_KEY=ENTER_STRIPE_PUBLIC_KEY_HERE

README.md

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
This Example Storefront is to serve as a reference on how to implement a web based storefront using the Reaction Commerce GraphQL API. You can fork this project as a jumping off point or create your own custom experience using your prefered client-side technology. While we believe our example storefront is full featured enough to use in production, it may be missing features your shop requires at this time.
66

77
## Features
8-
98
Reaction comes with a robust set of core commerce capabilities right out of the box. And since anything in our codebase can be extended, overwritten, or installed as a package, you may also customize anything on our platform.
109

1110
Check out the full list of Reaction [features](https://www.reactioncommerce.com/features) and [release history](https://reactioncommerce.com/roadmap) for more info.
@@ -26,11 +25,12 @@ This example storefront is built with [Next.js](https://nextjs.org/), [React](ht
2625
Follow the [Reaction Platform docs](https://docs.reactioncommerce.com/docs/installation-reaction-platform) to install and run all the services necessary to run the storefront:
2726

2827
| Directory: Service | URL |
29-
| --------------------------------------------------------------------------------------------- | ------------------------------------------------ |
28+
|-----------------------------------------------------------------------------------------------|--------------------------------------------------|
3029
| [`reaction`](https://github.com/reactioncommerce/reaction): GraphQL API | [localhost:3000/graphql](localhost:3000/graphql) |
3130
| [`reaction`](https://github.com/reactioncommerce/reaction): GraphQL Playground developer tool | [localhost:3000/graphql](localhost:3000/graphql) |
3231
| [`reaction`](https://github.com/reactioncommerce/reaction): Reaction Admin | [localhost:4080](localhost:4080) |
3332
| [`reaction`](https://github.com/reactioncommerce/reaction): MongoDB | [localhost:27017](localhost:27017) |
33+
| [`reaction-hydra`](https://github.com/reactioncommerce/reaction-hydra): Authentication | [localhost:4444](localhost:4444) |
3434
| [`example-storefront`](https://github.com/reactioncommerce/example-storefront): Storefront | [localhost:4000](localhost:4000) |
3535

3636
**Note**: The storefront has redirects so that if you open [http://localhost:4000/graphql](http://localhost:4000/graphql), you'll be redirected to the GraphQL Playground.
@@ -51,37 +51,32 @@ When running the storefront and Reaction for the first time, you will need to co
5151
Read the docs for [setting up Segment or a custom analytics tracker](docs/tracking-events.md)
5252

5353
## Documentation
54-
5554
- [Example Storefront full documentation](./docs)
5655
- [Example Storefront Component Library repository](https://github.com/reactioncommerce/reaction-component-library), [documentation](https://github.com/reactioncommerce/reaction-component-library/tree/master/docs), and [component documentation](http://designsystem.reactioncommerce.com/)
5756
- [Reaction Docs: Using GraphQL](https://docs.reactioncommerce.com/docs/graphql-using)
5857
- [Reaction Docs: Testing with Jest](https://docs.reactioncommerce.com/docs/testing-reaction)
59-
- [Reaction Docs: Developing with Docker](https://docs.reactioncommerce.com/docs/installation-docker-development)
58+
- [Reaction Docs: Developing with Docker](https://docs.reactioncommerce.com/docs/installation-docker-development
59+
)
6060

6161
## Development
6262

6363
The Reaction Platform runs the storefront with Docker, so you will have to use Docker commands to view logs, run commands inside the container and more. To run commands specifically for the storefront, make sure to change directories into the `example-storefront` directory within the `reaction-platform` repository:
64-
6564
```sh
6665
cd example-storefront
6766
```
6867

6968
### Build and run in development mode with logs
70-
7169
Create a symbolic link to use the development Docker image:
72-
7370
```
7471
ln -s docker-compose.dev.yml docker-compose.override.yml
7572
```
7673

7774
If running for the first time or environment variables in `.env.example` have changed execute the command below to update environment variables.
78-
7975
```
8076
./bin/setup
8177
```
8278

8379
Start the storefront by executing:
84-
8580
```sh
8681
docker-compose up -d && docker-compose logs -f
8782
```
@@ -99,31 +94,26 @@ docker-compose run --rm --service-ports web yarn start
9994
```sh
10095
docker-compose run --rm web [command]
10196
```
102-
10397
Run any command inside a Docker container and then remove the container. Use this to run any tooling operations. Remember your project directory will be mounted and things will usually just work. See Yarn section below for more examples.
10498

10599
### Run tests in container
106100

107101
Run tests locally
108-
109102
```sh
110103
docker-compose run --rm web yarn test
111104
```
112105

113106
Run tests locally without cache (this can be helpful if changes aren't showing up)
114-
115107
```sh
116108
docker-compose run --rm web yarn test --no-cache
117109
```
118110

119111
To run Snyk security tests (this will run tests in the same way as CI)
120-
121112
```sh
122113
docker-compose run --rm web sh -c "cp package.json ../ && cp .snyk ../ && cd .. && snyk auth && snyk test"
123114
```
124115

125116
To run ESLint
126-
127117
```sh
128118
docker-compose run --rm web eslint src
129119
```
@@ -139,15 +129,13 @@ You can use the Google Chrome DevTools to debug the code running in the Node.js
139129

140130
Yarn & NPM should run inside the Docker container. We've taken steps to ensure that the node_modules are placed into a cacheable location. If you run Yarn locally, the node_modules are written directly to the project directory and take precedence over those from the Docker build.
141131
**Yarn Add**
142-
143132
```
144133
docker-compose run --rm web yarn add --dev [package]
145134
```
146135

147136
**Yarn Install**
148137

149138
⚠️ Always rebuild the image and start a new container after modifying yarn.lock or Dockerfile!
150-
151139
```
152140
docker-compose run --rm web yarn install
153141
docker-compose down --rmi local
@@ -167,29 +155,25 @@ Sometimes we need to test the [Example Storefront Component Library](https://git
167155
1. Inside your `.env` file, change `INTERNAL_GRAPHQL_URL` to equal `http://localhost:3000/graphql`, the same as the `EXTERNAL_GRAPHQL_URL`
168156
1. Start the storefront locally by running the command `export $(cat .env | xargs) && yarn dev`
169157
1. Your storefront should now be running at `localhost:4000`
170-
- If you see errors about not being able to find peer dependency packages, that seems to be an issues with yarn linking. You can just temporarily `yarn add` each of those packages in the component library `package/dist` folder. (This folder is gitignored anyway.)
158+
- If you see errors about not being able to find peer dependency packages, that seems to be an issues with yarn linking. You can just temporarily `yarn add` each of those packages in the component library `package/dist` folder. (This folder is gitignored anyway.)
171159
1. After your changes are tested, shut down the storefront by running the command `CTRL+C`
172160
1. Run `yarn unlink "@reactioncommerce/components"` in the storefront repo folder
173161
1. `cd` to the `package/dist` folder of the `reaction-component-library` repo. Run the command `yarn unlink` to unlink the local version of the component library
174162
1. Undo the renaming of your `.yarnrc` file
175163
1. Undo the URL change inside your `.env` file
176164

177165
## Clean up containers
178-
179166
Stop, and retain containers:
180-
181167
```sh
182168
docker-compose stop
183169
```
184170

185171
Stop, and remove containers:
186-
187172
```sh
188173
docker-compose down
189174
```
190175

191176
Stop, and remove containers, volumes and built images:
192-
193177
```sh
194178
docker-compose down -v --rmi local
195179
```
@@ -203,7 +187,6 @@ Run this command to build a Docker image with the production build of the app in
203187
```sh
204188
docker build --network=host -t reactioncommerce/example-storefront:X.X.X .
205189
```
206-
207190
Where X.X.X indicates the tag version you want to use, i.e. `3.1.0`
208191

209192
Then, to start the app on your machine, make sure the Reaction API container is already running and enter:
@@ -231,37 +214,28 @@ Want to request a feature? Use our [Reaction Feature Requests repository](https:
231214
We love your pull requests! Check our our [`Good First Issue`](https://github.com/reactioncommerce/example-storefront/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22) and [`Help Wanted`](https://github.com/reactioncommerce/example-storefront/issues?q=label%3A%22help+wanted%22) tags for good issues to tackle.
232215

233216
### Pull Request guidelines
234-
235217
Pull requests should pass all automated tests, style, and security checks.
236218

237219
#### Automated Tests
238-
239220
Your code should pass all [acceptance tests and unit tests](https://docs.reactioncommerce.com/reaction-docs/trunk/testing-reaction). Run
240-
241221
```sh
242222
docker-compose run --rm web yarn test
243223
```
244-
245224
to run the test suites locally. If you're adding functionality to Reaction, you should add tests for the added functionality. You can run the tests locally without cache if necessary by passing the `--no-cache` flag. This can be helpful if changes aren't showing up.
246-
247225
```sh
248226
docker-compose run --rm web yarn test --no-cache
249227
```
250228

251229
To update a failing snapshot (if you've made changes to a component)
252-
253230
```sh
254231
docker-compose run --rm web yarn test -u
255232
```
256233

257234
#### Eslint
258-
259235
We require that all code contributed to Reaction follows [Reaction's ESLint rules](https://github.com/reactioncommerce/reaction-eslint-config). You can run
260-
261236
```
262237
docker-compose run --rm web eslint src
263238
```
264-
265239
to run ESLint against your code locally.
266240

267241
Please follow the [Reaction Code Style Guide](https://docs.reactioncommerce.com/docs/styleguide). Check out our guides to [JSDoc](https://docs.reactioncommerce.com/docs/jsdoc-style-guide), [Git](https://docs.reactioncommerce.com/docs/git-style-guide), [error handling](https://docs.reactioncommerce.com/docs/error-handling-guide), [logging](https://docs.reactioncommerce.com/docs/logging), and [React](https://docs.reactioncommerce.com/docs/react-best-practices).
@@ -271,9 +245,7 @@ We also request that you follow the our [pull request template](https://docs.rea
271245
Get more details in our [Contributing Guide](https://docs.reactioncommerce.com/docs/contributing-to-reaction).
272246

273247
### Developer Certificate of Origin
274-
275248
We use the [Developer Certificate of Origin (DCO)](https://developercertificate.org/) in lieu of a Contributor License Agreement for all contributions to Reaction Commerce open source projects. We request that contributors agree to the terms of the DCO and indicate that agreement by signing all commits made to Reaction Commerce projects by adding a line with your name and email address to every Git commit message contributed:
276-
277249
```
278250
Signed-off-by: Jane Doe <[email protected]>
279251
```
@@ -286,20 +258,20 @@ We use the [Probot DCO GitHub app](https://github.com/apps/dco) to check for DCO
286258

287259
If you forget to sign your commits, the DCO bot will remind you and give you detailed instructions for how to amend your commits to add a signature.
288260

289-
## License
290261

291-
Copyright 2019 Reaction Commerce
262+
## License
263+
Copyright 2019 Reaction Commerce
292264

293-
Licensed under the Apache License, Version 2.0 (the "License");
294-
you may not use this file except in compliance with the License.
295-
You may obtain a copy of the License at
265+
Licensed under the Apache License, Version 2.0 (the "License");
266+
you may not use this file except in compliance with the License.
267+
You may obtain a copy of the License at
296268

297269
http://www.apache.org/licenses/LICENSE-2.0
298270

299-
Unless required by applicable law or agreed to in writing, software
300-
distributed under the License is distributed on an "AS IS" BASIS,
301-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
302-
See the License for the specific language governing permissions and
303-
limitations under the License.
271+
Unless required by applicable law or agreed to in writing, software
272+
distributed under the License is distributed on an "AS IS" BASIS,
273+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
274+
See the License for the specific language governing permissions and
275+
limitations under the License.
304276

305277
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Freactioncommerce%2Fexample-storefront.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Freactioncommerce%2Fexample-storefront?ref=badge_large)

apiUtils/localeMiddleware.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import redirect from "./redirect";
33

44
export default (req, res) => {
55
const {
6-
query: { slug, ...rest }
6+
query: { slug },
7+
_parsedUrl
78
} = req;
89

910
const fallback = "de";
@@ -40,7 +41,7 @@ export default (req, res) => {
4041
found = fallback;
4142
}
4243

43-
const queryPart = rest ? (`?${Object.keys(rest).map((k) => `${k}=${rest[k]}`).join("&")}`) : "";
44+
const queryPart = (_parsedUrl && _parsedUrl.query) ? `?${_parsedUrl.query}` : "";
4445

4546
if (slug) {
4647
return redirect(res, 302, `/${found}${slug ? `/${slug.join("/")}` : ""}${queryPart}`);

apiUtils/passportMiddleware.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import OAuth2Strategy from "passport-oauth2";
2+
import passport from "passport";
3+
import sessions from "client-sessions";
4+
import appConfig from "../config.js";
5+
import redirect from "./redirect";
6+
7+
export { default as passport } from "passport";
8+
9+
let baseUrl = appConfig.CANONICAL_URL;
10+
if (!baseUrl.endsWith("/")) baseUrl = `${baseUrl}/`;
11+
12+
const oauthRedirectUrl = `${baseUrl}callback`;
13+
14+
// This is needed to allow custom parameters (e.g. loginActions) to be included
15+
// when requesting authorization. This is setup to allow only loginAction to pass through
16+
OAuth2Strategy.prototype.authorizationParams = function (options = {}) {
17+
return { loginAction: options.loginAction };
18+
};
19+
20+
passport.use("oauth2", new OAuth2Strategy({
21+
authorizationURL: appConfig.OAUTH2_AUTH_URL,
22+
tokenURL: appConfig.OAUTH2_TOKEN_URL,
23+
clientID: appConfig.OAUTH2_CLIENT_ID,
24+
clientSecret: appConfig.OAUTH2_CLIENT_SECRET,
25+
callbackURL: oauthRedirectUrl,
26+
state: true,
27+
scope: ["offline", "openid"]
28+
}, (accessToken, refreshToken, params, profile, cb) => {
29+
cb(null, { accessToken, refreshToken, idToken: params.id_token });
30+
}));
31+
32+
passport.serializeUser((user, done) => {
33+
done(null, JSON.stringify(user));
34+
});
35+
36+
passport.deserializeUser((user, done) => {
37+
done(null, JSON.parse(user));
38+
});
39+
40+
export default (handler) => (req, res) => {
41+
if (!res.redirect) {
42+
res.redirect = (location) => redirect(res, 302, location);
43+
}
44+
45+
sessions({
46+
cookieName: "session", // This name is required so passport picks it up correctly
47+
secret: appConfig.SESSION_SECRET,
48+
duration: appConfig.SESSION_MAX_AGE_MS
49+
})(req, res, () =>
50+
passport.initialize()(req, res, () =>
51+
passport.session()(req, res, () =>
52+
handler(req, res))));
53+
};

bin/start

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ IFS="$(printf "\n\t")"
88

99
cd "$(dirname "$0")/.."
1010
yarn install
11-
12-
yarn start:dev
11+
printf "Waiting for Hydra service... "
12+
./bin/wait-for.sh "${OAUTH2_HOST}:${OAUTH2_ADMIN_PORT}"
13+
printf "Hydra service found!\n"
14+
yarn start:dev

0 commit comments

Comments
 (0)