Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert: Quick fix for expired access token problem in web component #1048

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Fixed

- Revert: Quick fix for expired access token problem in web component (#1048)

## [0.25.3] - 2024-06-19

### Fixed
Expand Down
3 changes: 1 addition & 2 deletions cypress/e2e/spec-wc.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ describe("default behaviour", () => {
describe("when load_remix_disabled is true, e.g. in editor-standalone", () => {
const authKey = `oidc.user:https://auth-v1.raspberrypi.org:editor-api`;

const oneHourFromNow = (new Date().getTime() + 60 * 60 * 1000) / 1000;
const user = { access_token: "dummy-access-token", expires_at: oneHourFromNow };
const user = { access_token: "dummy-access-token" };
const originalIdentifier = "blank-python-starter";

const urlFor = (identifier) => {
Expand Down
22 changes: 3 additions & 19 deletions src/containers/WebComponentLoader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,13 @@ const WebComponentLoader = (props) => {
useEditorStyles = false, // If true use the standard editor styling for the web component
} = props;

const getLocalStorageUser = (authKey) => {
if (authKey) {
const user = JSON.parse(localStorage.getItem(authKey));
if (user) {
const expiresAt = new Date(user.expires_at * 1000);
if (new Date() < expiresAt) {
return user;
} else {
return null;
}
} else {
return null;
}
} else {
return null;
}
};

const dispatch = useDispatch();
const { t } = useTranslation();
const [projectIdentifier, setProjectIdentifier] = useState(identifier);
localStorage.setItem("authKey", authKey);
const localStorageUser = getLocalStorageUser(authKey);
const localStorageUser = authKey
? JSON.parse(localStorage.getItem(authKey))
: null;
const user = useSelector((state) => state.auth.user || localStorageUser);
const [loadCache, setLoadCache] = useState(!!!user);
const [loadRemix, setLoadRemix] = useState(!!user);
Expand Down
48 changes: 1 addition & 47 deletions src/containers/WebComponentLoader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ const identifier = "My amazing project";
const steps = [{ quiz: false, title: "Step 1", content: "Do something" }];
const instructions = { currentStepPosition: 3, project: { steps: steps } };
const authKey = "my_key";
const oneHourFromNow = (new Date().getTime() + 60 * 60 * 1000) / 1000;
const user = { access_token: "my_token", expires_at: oneHourFromNow };
const user = { access_token: "my_token" };

describe("When no user is in state", () => {
beforeEach(() => {
Expand Down Expand Up @@ -233,51 +232,6 @@ describe("When no user is in state", () => {
});
});

describe("with user set in local storage, but access token has expired", () => {
beforeEach(() => {
const oneHourAgo = (new Date().getTime() - 60 * 60 * 1000) / 1000;
user.expires_at = oneHourAgo;

localStorage.setItem(authKey, JSON.stringify(user));
localStorage.setItem("authKey", authKey);
render(
<Provider store={store}>
<CookiesProvider cookies={cookies}>
<WebComponentLoader
code={code}
identifier={identifier}
senseHatAlwaysEnabled={true}
instructions={instructions}
authKey={authKey}
theme="light"
/>
</CookiesProvider>
</Provider>,
);
});

test("Calls useProject hook as if user was not set in local storage", () => {
expect(useProject).toHaveBeenCalledWith({
projectIdentifier: identifier,
code,
accessToken: undefined,
loadRemix: false,
loadCache: true,
remixLoadFailed: false,
});
});

test("Calls useProjectPersistence hook as if user was not set in local storage", () => {
expect(useProjectPersistence).toHaveBeenCalledWith({
user: null,
project: { components: [] },
hasShownSavePrompt: true,
justLoaded: false,
saveTriggered: false,
});
});
});

describe("When theme is not set", () => {
beforeEach(() => {
render(
Expand Down
Loading