Authorization

Authorization

increment is declared with requires authorized; authorize checks the password and sets a flag. Until you authorize successfully, the increment action will not run—per-action authorization on the channel.

Backend

from rxdjango import ContextChannel, rx, action


class AuthorizationChannel(ContextChannel):

    authorized = rx[bool](False)
    counter = rx[int](0)

    @action
    async def authorize(self, password: str):
        if password == 'password':
            self.authorized = True
            return True
        return False

    @action(requires='authorized')
    async def increment(self):
        self.counter += 1

Frontend

import React, { useState } from 'react';
import { useChannel } from '@rxdjango/react';
import { AuthorizationChannel } from '../../rx/authorization/authorization.channels';
import {
  Sections,
  Fields,
  Field,
  Button,
  TextInput,
  Row,
  Note,
} from '../../components/demo';

export function AuthorizationDemo() {
  const channel = useChannel(AuthorizationChannel);
  const [password, setPassword] = useState('password');

  return (
    <Sections>
      <div>
        <Note>
          Authorize with a password, then use the counter.
        </Note>
        <Row>
          <TextInput
            id="authorization-password"
            label="Password"
            value={password}
            onChange={setPassword}
          />
          <Button
            variant="secondary"
            onClick={() => channel.authorize(password)}
          >
            Authorize
          </Button>
        </Row>
      </div>
      <div>
        <Fields>
          <Field label="Counter value" large>
            {channel.counter}
          </Field>
        </Fields>
        <Button onClick={channel.increment}>
          Increment
        </Button>
      </div>
    </Sections>
  );
}

export default AuthorizationDemo;