Skip to content

Commit

Permalink
TRICLOPS-13: Present sign in and sign out links (including developer …
Browse files Browse the repository at this point in the history
…sign-in)
  • Loading branch information
elohanlon committed Jun 28, 2024
1 parent 776bd9b commit 5fa9787
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
22 changes: 22 additions & 0 deletions app/javascript/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,41 @@ import React, { useState, useEffect } from 'react';

const App = () => {
const [appVersion, setAppVersion] = useState<string | null>(null);
const [userSignedIn, setUserSignedIn] = useState<boolean>(false);

useEffect(() => {
setAppVersion(document.body.getAttribute('data-app-version'));
setUserSignedIn(document.body.getAttribute('data-user-signed-in') == 'true');
}, [appVersion])

if (!appVersion) {
return 'Loading...';
}

function renderUserSignInLinks() {
return userSignedIn
? (
<form action="/users/sign_out" method="post">
<input type="hidden" name="_method" value="delete" />
<input type="hidden"
name={
document.head.querySelector("[name='csrf-param']").content
}
value={
document.head.querySelector("[name='csrf-token']").content
}
/>
<input type="submit" name="commit" value="Sign out" />
</form>
)
: <a href="/users/sign_in">Sign in</a>;
}

return (
<div>
<h1>Triclops</h1>
<p>{`Version ${appVersion}`}</p>
{renderUserSignInLinks()}
</div>
);
};
Expand Down
11 changes: 11 additions & 0 deletions app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h2>Log in</h2>

<%= render "devise/shared/links" %>

<% if Rails.env.development? %>
<p>
<strong>Development environment only:</strong>
<br /><br />
<%= link_to 'Sign in with development account', users_development_sign_in_developer_path %>
</p>
<% end %>
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<%= vite_typescript_tag 'application.tsx' %>
</head>

<body data-app-version="<%= APP_VERSION %>">
<body data-app-version="<%= APP_VERSION %>" data-user-signed-in="<%= user_signed_in? %>">
<% flash.each do |type, msg| %>
<div class="alert"><%= msg %></div>
<% end %>
Expand Down

0 comments on commit 5fa9787

Please sign in to comment.