Signed-in learners
Without a session, LearnHouseActivity is read-only and anonymous: it shows public courses and saves nothing. Give it
an embed session and it acts as one of your learners:
- a Mark as complete toggle, saved to their LearnHouse progress (and shown as done when they come back),
- code playground runs and saved submissions,
- assignments: answering tasks, uploading files, submitting, retrying and seeing their grade,
- private courses they are enrolled in, and blocks that show who they are.
An embed session is a token your server mints for one learner with your org's API token. It is short-lived, has no refresh token, and works only for what the components do. Your API token never leaves your server.
Mint a session on your server
Create an API token under Developers → API Access with the Users: update permission, then add a route that returns a session for the signed-in user of your app:
// app/api/learnhouse-session/route.ts (Next.js), or any server route import { createClient } from '@learnhouse/sdk' const lh = createClient({ baseUrl: process.env.LEARNHOUSE_URL!, token: process.env.LEARNHOUSE_TOKEN! }) export async function POST() { const me = await getCurrentUser() // your own auth if (!me) return new Response(null, { status: 401 }) const session = await lh.embed.createSession({ orgSlug: 'your-school', userId: me.learnhouseUserId, // the LearnHouse user id you stored for them }) return Response.json({ access_token: session.access_token, expires_at: session.expires_at }) }
createSession refuses to run in a browser. It calls POST /api/v1/admin/{org_slug}/auth/embed-session, so any
backend language works: send { "user_id": 42 } with Authorization: Bearer lh_….
Hand it to the provider
<LearnHouseProvider baseUrl="https://learn.your-school.com" getSession={() => fetch('/api/learnhouse-session', { method: 'POST' }).then((r) => r.json())} > <LearnHouseActivity activityId="activity_…" courseId="course_…" /> </LearnHouseProvider>
getSession runs when the provider mounts, about a minute before each session expires, and once more if the API
answers 401. Every activity under the provider shares the one session.
If you already have a session (rendered on the server, say), pass it as session instead, alone or alongside
getSession. Both take the object createSession returns or just its access_token.
What learners can do
| Without a session | With a session | |
|---|---|---|
| Public activities | Yes | Yes |
| Private courses | No | Those the learner can open in LearnHouse |
| Completion | complete | Saved to their progress |
| Code playground | Shown, can't run code | Runs code and saves their submissions |
| Assignments | Not shown | Full learner view: answer, upload, submit, retry, grade |
| User and dynamic blocks | Generic | Personalised |
Enrol learners first, from your server, with lh.admin.enrollments.enroll()
(POST /api/v1/admin/{org_slug}/enrollments/{user_id}/{course_uuid}). An embed session does not enrol anyone on its
own.
Completion
With a session, a Mark as complete toggle appears below each activity. Change it with completion:
completion | |
|---|---|
button | The toggle (default) |
scroll | Marked complete when the end of the activity scrolls into view |
none | Nothing; call useLearnHouseClient().trail.completeActivity(id) |
Assignments have no toggle: they are complete when submitted. Every change fires a progress event:
<LearnHouseActivity activityId="activity_…" onEvent={(e) => e.type === 'progress' && refreshMyCourseSidebar(e.activityId, e.completed)} />
Progress hooks
useTrail(orgId) and useCourseProgress(orgId, course) give you the learner's progress as React state, for a course
header, a sidebar or a "continue where you left off" card. They share one copy of the trail with every activity's
Mark as complete button under the same provider: it loads once, and a change made anywhere shows up everywhere
without another request.
import { useCourseProgress } from '@learnhouse/react' export function CourseHeader({ orgId, courseUuid }: { orgId: number; courseUuid: string }) { const { progress, loading, error } = useCourseProgress(orgId, courseUuid) if (loading || !progress) return <p>Loading…</p> if (error) return <p>Progress unavailable</p> return ( <p> {progress.completed} of {progress.total} lessons ({progress.percent}%) </p> ) }
useCourseProgress(orgId, course) (course: the course's numeric id or uuid) returns:
| Field | |
|---|---|
progress | CourseProgressenrolledcompletedtotalpercentcompletedActivityIdsnull |
loadingerror | Load state of the shared trail |
complete(activityUuid)uncomplete(activityUuid) | Change completion; every hook and button updates |
enroll()leave() | Need the course uuid and a full LearnHouse sign-in. Embed sessions can't enroll: do it from your server |
isCompleted(activityId) | By numeric activity id |
refresh() | Load the trail again |
useTrail(orgId) returns { trail, loading, error, refresh }: trail is undefined until loaded and null when
signed out or unavailable. Pass null as orgId until you know it. Both hooks need <LearnHouseProvider>.
Who is signed in
useLearnHouseSession() returns the provider's session: status (loading, authenticated, unauthenticated) and
data.user, read from LearnHouse once per learner.
When a session can't be renewed
If getSession fails or returns nothing, the components keep working signed out and fire session_expired. Listen
for it to send the learner back through your own sign-in.
Security
Never send an lh_ API token to a browser. The components throw if you pass one as a session.
An embed session is deliberately narrower than a LearnHouse sign-in:
- Short-lived. 15 minutes by default; pass
ttlSeconds(60 to 3600) tocreateSessionto change it. There is no refresh token: when it runs out, your server decides whether to mint another. - Only the embed's routes. It reads courses, activities and the learner's own progress, submissions and grade, and
writes only completion, assignment answers and code runs. Every other API route answers
403, whatever the learner could do signed in to LearnHouse. - One organization. It works only in the organization that minted it.
- Never the account. It can't change the learner's email, password or second factor, or delete the account.
- Learners only. Minting is refused for admins, maintainers, accounts with more rights than the API token, and accounts that belong to other organizations.
Signing out of LearnHouse, or changing the password, ends every embed session of that learner too.
Your server vouches for the learner, the way an LMS does for an LTI launch. So inside your organization, embed sessions are not held to its allowed sign-in methods or its two-factor requirement: protect the route that mints them with your own sign-in.
Cross-origin requests
LearnHouse answers browser requests from any origin when they carry a bearer token and no cookies, so the components
work on your own domain. A visitor's LearnHouse cookie session stays unreadable from other sites, as before.
Self-hosters can turn this off with LEARNHOUSE_BEARER_CORS=false; the API then only answers its own domains.